package.jsonnode
{ "name": "first_global_module", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "test" }, "bin": { "fgm": "index.js" }, "keywords": [ "" ], "author": "jackple", "license": "ISC", }
index.jsnpm
#!/usr/bin/env node var w = process.argv[2], h = process.argv[3]; w = Number(w); h = Number(h); console.log(w, h);
發佈模塊json
npm version [number] npm publish
安裝全局模塊code
npm i first_global_module@1.0.0 -g
運行ip
fgm 1 1
說明:io
package.json中的fgm指定全局模塊的使用命令console
若是版本爲第一次發佈,可直接執行發佈命令npm publish,不然須要先修改版本號再發布test
fgm 1 2命令的參數下標由0開始,實際執行爲node ./index.js 1 1, process.argv[2]爲獲取第一個參數1)module
"#!/usr/bin/env node"指定文件由node執行package.json