全局添加可执行的node脚本

在本地  npm 包项目下 package.json 文件中添加脚本

{
  "name": "winyh",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "bin": "bin/run",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {}
}

  

{
  ...
 "bin": "bin/run"
 ...
}

  

创建目录bin, 并在bin中创建文件run,这便是本地npm包的可执行文件。

编辑run 文件

#!/usr/bin/env node

console.log('winyh script run success!');

  

包根目录执行(链接)命令

npm link

  

此时,在任意一个位置终端里执行以下命令都可以看到输出  winyh script run success!

winyh

  

原文地址:https://www.cnblogs.com/winyh/p/14081642.html