我无法理解该-g标志在 NPM 中的工作原理。具体来说,我正在努力理解它与 NPM 模块公开的命令行功能有何关系。
我认为本地安装包和全局安装包之间的区别很简单,本地包在特定项目之外不可用。当然,全局安装的包在任何项目中都可用。我有 Rails 背景,所以对我来说这类似于将 gem 安装到特定 RVM 中与将其安装到全局 RVM 中。它只会影响它可用的地方。
然而,NPM 的意义似乎不仅仅是范围。对于具有命令行功能的软件包,例如wait-on,该软件包(据我所知)在命令行上不可用,除非它是全局安装的。
$ npm install wait-on
$ wait-on
=> -bash: /usr/local/bin/wait-on: No such file or directory
Run Code Online (Sandbox Code Playgroud)
$ npm install wait-on -g
$ wait-on
=> Usage: wait-on {OPTIONS} resource [...resource]
Description:
wait-on is a command line utility which will wait for files, ports,
sockets, and http(s) resources to become available (or not available
using reverse flag). Exits with success code (0) when all resources
are ready. Non-zero exit code if interrupted or timed out.
Options may also be specified in a config file (js or json). For
example --config configFile.js would result in configFile.js being
required and the resulting object will be merged with any
Run Code Online (Sandbox Code Playgroud)
是否可以在本地安装同时获得命令行功能?这对我的 CI 设置非常有帮助,因为缓存本地模块比全局模块要容易得多,因此在可能的情况下我更愿意在本地安装。
小智 5
如果您使用 npm 5.2.0 或更高版本,npx则默认包含该命令。它将允许您从本地节点模块运行:npx wait-on
供参考: https: //www.npmjs.com/package/npx