繁体   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