package.json bin的作用
許多包有一個或多個可執行檔案(executable),他們希望直接匯入到全域性路徑裡面,這樣可以直接使用,npm很容易達到這點,
A lot of packages have one or more executable files that they’d like to install into the PATH. npm makes this pretty easy (in fact, it uses this feature to install the “npm” executable.)
使用這個,在package.json提供一個對映到本地本地檔名的bin欄位,一旦被引入後,npm將軟連結這個檔案到prefix/bin裡面,以便於全域性引入,或者在./node_modules/.bin/目錄裡
To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
比如,myapp可能像這樣:
For example, myapp could have this:
{ "bin" : { "myapp" : "./cli.js" } }
所以,當你引入myapp時,他建立了一個軟連結到 cli.js檔案
So, when you install myapp, it’ll create a symlink from the cli.js script to
/usr/local/bin/myapp
如果你有一個單一可執行檔案,他的名字應該是和package名字一樣,那樣你就可以,想使用字串一樣使用它,比如:
If you have a single executable, and its name should be the name of the package, then you can just supply it as a string. For example:
{ "name": "my-program"
, "version": "1.2.5"
, "bin": "./path/to/program" }
would be the same as this:
{ "name": "my-program"
, "version": "1.2.5"
, "bin" : { "my-program" : "./path/to/program" } }
請確保你的bin檔案裡面最開頭寫上 #!/usr/bin/env node,否則檔案裡的指令碼不會再Node環境下執行
Please make sure that your file(s) referenced in bin starts with #!/usr/bin/env node, otherwise the scripts are started without the node executable!