node.js 如何判断 npm 包是全局安装还是本地安装
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26104276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to tell if npm package was installed globally or locally
提问by AndraeRay
I am installing grunt, node, npm, bower, and grunt-cli on windows7.
我正在 windows7 上安装 grunt、node、npm、bower 和 grunt-cli。
The instructions say i should run the install commands with -g flag for global.
说明说我应该使用 -g 标志为全局运行安装命令。
How can I check if I used the -g flag when i installed. It will take a lot of time to uninstall them and reinstall.
如何检查我在安装时是否使用了 -g 标志。卸载它们并重新安装需要很多时间。
回答by Muntaser Ahmed
Use the listcommand with the -gflag to see all packages that are installed globally:
使用list带有-g标志的命令查看全局安装的所有软件包:
npm list -g
npm list -g
To check if a specific package is installed globally, you can provide the name of package (gruntin this case) as seen below:
要检查是否全局安装了特定包,您可以提供包的名称(grunt在这种情况下),如下所示:
npm list -g grunt
npm list -g grunt
Or you can use grepto filter on package names:
或者您可以使用grep过滤包名称:
npm list -g | grep grunt
npm list -g | grep grunt
Source: https://docs.npmjs.com/cli/ls
回答by Flimm
npm list --depth 1 --global packagename > /dev/null 2>&1
You can then check the exit status to see if it's installed or not. Thanks Adam Monsen.
然后,您可以检查退出状态以查看它是否已安装。谢谢亚当蒙森。
回答by mvermand
To check if a specific package is installed globally execute:
要检查是否全局安装了特定包,请执行:
npm list -g [package-name]
Let's take "grunt" as an example. If it is installed globally, you should see something like this
我们以“咕噜”为例。如果它是全局安装的,您应该会看到类似这样的内容
C:\data\tryout\test1>npm list -g grunt
C:\Users\xxxxxxx\AppData\Roaming\npm
└── [email protected]
If it is not installed globally, you should see something like this
如果没有全局安装,你应该看到这样的
C:\data\tryout\test1>npm list -g grunt
C:\Users\xxxxxxx\AppData\Roaming\npm
└── (empty)
To check if a specific package is installed locally you can execute the same commands as above but without the -g parameter.
要检查本地是否安装了特定软件包,您可以执行与上述相同的命令,但不带 -g 参数。
source: How to check if npm package was installed globally or locally.
回答by Mwiza
You can list all global packages with the command:
您可以使用以下命令列出所有全局包:
npm ls -g
Or check for a specific package with:
或者使用以下命令检查特定包:
npm ls -g [package-name]
For example: npm ls -g @angular/cli
例如: npm ls -g @angular/cli


