簡體   English   中英

如何創建一個可以全局安裝的nodejs命令行工具?

[英]How to create a nodejs-command-line-tool that it can be installed globally?

如何構建/安裝可以在全球范圍內使用的節點命令行工具?

我編寫了一個名為“ extractedeps ”的命令行實用程序,該實用程序將從package.json文件中列出了來自節點應用程序的所有依賴項。

與“ jest”之類的其他節點實用程序不同,我無法在shell中全局執行它而不輸入其具體路徑。

安裝示例:

$ npm install --verbose -g extractdeps -g
npm info it worked if it ends with ok
npm verb cli [ '/usr/local/Cellar/node/8.9.1/bin/node',
npm verb cli   '/usr/local/bin/npm',
npm verb cli   'install',
npm verb cli   '--verbose',
npm verb cli   '-g',
npm verb cli   'extractdeps',
npm verb cli   '-g' ]
npm info using npm@5.5.1
npm info using node@v8.9.1
npm verb npm-session 5cff952d9a7f0be7
npm http fetch GET 200 https://registry.npmjs.org/extractdeps 19ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/co 14ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/commander 16ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/fs 15ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/shelljs 16ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/glob 6ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/interpret 8ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/rechoir 8ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/fs.realpath 20ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/inflight 21ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/once 20ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/minimatch 21ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/inherits 22ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/path-is-absolute 21ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/wrappy 5ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/brace-expansion 4ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/balanced-match 5ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/concat-map 5ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/resolve 3ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/path-parse 4ms (from cache)
npm verb correctMkdir /Users/hagen/.npm/_locks correctMkdir not in flight; initializing
npm verb lock using /Users/hagen/.npm/_locks/staging-3a08f0df5026584d.lock for /usr/local/lib/node_modules/.staging
npm info lifecycle extractdeps@1.0.0~preuninstall: extractdeps@1.0.0
npm info lifecycle extractdeps@1.0.0~uninstall: extractdeps@1.0.0
npm verb unbuild rmStuff extractdeps@1.0.0 from /usr/local/lib/node_modules
npm info lifecycle extractdeps@1.0.0~postuninstall: extractdeps@1.0.0
npm info lifecycle extractdeps@1.0.0~preinstall: extractdeps@1.0.0
npm info linkStuff extractdeps@1.0.0
npm verb linkBins extractdeps@1.0.0
npm verb linkMans extractdeps@1.0.0
npm info lifecycle extractdeps@1.0.0~install: extractdeps@1.0.0
npm info lifecycle extractdeps@1.0.0~postinstall: extractdeps@1.0.0
npm verb unlock done using /Users/hagen/.npm/_locks/staging-3a08f0df5026584d.lock for /usr/local/lib/node_modules/.staging
+ extractdeps@1.0.0
updated 1 package in 0.764s
npm verb exit [ 0, true ]
npm info ok

但是執行失敗:

$ extractdeps
-bash: extractdeps: command not found

如果我對其他工具(如jest)執行相同的操作,則效果很好:

$ jest
-bash: jest: command not found

$ npm install -g jest
/usr/local/bin/jest -> /usr/local/lib/node_modules/jest/bin/jest.js

$ jest -v
No tests found

我在這里錯過了什么?

因此,我剛剛在全球范圍內安裝了該軟件包,並注意到了同樣的情況。 我想知道是否需要在package.json指定"bin"標志。 看起來它正在安裝模塊,但是它不知道要添加到/usr/local/bin

例如,在grunt-cli package.json中:

"bin": {
    "grunt": "bin/grunt"
 },

更新:做了一些研究,這確實是個問題。 https://docs.npmjs.com/files/package.json#bin

基本上,當您指定此標志並進行全局安裝時,npm會在本地bin中創建一個指向您指定的cli入口點的符號鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM