node.js 如何使用 npm 全局安装模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5817874/
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 do I install a module globally using npm?
提问by Cristian
I recently installed Node.js and npm module on OSX and have a problem with the settings I think:
我最近在 OSX 上安装了 Node.js 和 npm 模块,但我认为设置有问题:
npm install [MODULE] is not installing the node.js module to the default path
which is /usr/local/lib/node_modules.
回答by schaermu
If you want to install a npm module globally, make sure to use the new -gflag, for example:
如果要全局安装 npm 模块,请确保使用新-g标志,例如:
npm install forever -g
npm install forever -g
The general recommendations concerning npm module installation since 1.0rc (taken from blog.nodejs.org):
自 1.0rc 以来关于 npm 模块安装的一般建议(取自blog.nodejs.org):
- If you're installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
- If you're installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
- 如果您要安装要在程序中使用的东西,请使用 require('whatever'),然后将其安装在本地,位于项目的根目录下。
- 如果您要在 shell、命令行或其他地方安装要在 shell 中使用的内容,请全局安装,以便其二进制文件最终出现在您的 PATH 环境变量中。
I just recently used this recommendations and it went down pretty smoothly. I installed forever globally (since it is a command line tool) and all my application modules locally.
我最近刚刚使用了这个建议,它很顺利地下降了。我在全球范围内永久安装(因为它是一个命令行工具)和我本地的所有应用程序模块。
However, if you want to use some modules globally (i.e. express or mongodb), take this advice (also taken from blog.nodejs.org):
但是,如果您想全局使用某些模块(即 express 或 mongodb),请采纳以下建议(也取自blog.nodejs.org):
Of course, there are some cases where you want to do both. Coffee-script and Express both are good examples of apps that have a command line interface, as well as a library. In those cases, you can do one of the following:
- Install it in both places. Seriously, are you that short on disk space? It's fine, really. They're tiny JavaScript programs.
- Install it globally, and then npm link coffee-script or npm link express(if you're on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.
The first option is the best in my opinion. Simple, clear, explicit. The second is really handy if you are going to re-use the same library in a bunch of different projects. (More on npm link in a future installment.)
当然,在某些情况下,您希望两者都做。Coffee-script 和 Express 都是具有命令行界面和库的应用程序的好例子。在这些情况下,您可以执行以下操作之一:
- 在这两个地方安装它。说真的,你的磁盘空间那么短缺吗?没关系,真的。它们是很小的 JavaScript 程序。
- 全局安装它,然后 npm link coffee-script 或 npm link express(如果你在支持符号链接的平台上。)然后你只需要更新全局副本来更新所有符号链接。
第一个选项在我看来是最好的。简单、清晰、明确。如果您要在一堆不同的项目中重复使用相同的库,则第二个非常方便。(在以后的文章中更多关于 npm 链接。)
I did not test one of those variations, but they seem to be pretty straightforward.
我没有测试这些变体之一,但它们似乎非常简单。
回答by Droogans
On a Mac, I found the output contained the information I was looking for:
在 Mac 上,我发现输出包含我正在寻找的信息:
$> npm install -g karma
...
...
> [email protected] install /usr/local/share/npm/lib/node_modules/karma/node_modules/socket.io/node_modules/socket.io-client/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)
...
$> ls /usr/local/share/npm/bin
karma nf
After adding /usr/local/share/npm/binto the export PATHline in my .bash_profile, saving it, and sourceing it, I was able to run
添加/usr/local/share/npm/bin到export PATHmy 中的行后.bash_profile,保存并source运行它,我能够运行
$> karma --help
normally.
一般。
回答by fullstacklife
I like using a package.json file in the root of your app folder.
我喜欢在应用程序文件夹的根目录中使用 package.json 文件。
Here is one I use
这是我使用的一个
nvm use v0.6.4
npm install
回答by Cody
I had issues installing Express on Ubuntu:
我在 Ubuntu 上安装 Express 时遇到问题:
If for some reason NPM command is missing, test npm command with npm help. If not there, follow these steps - http://arnolog.net/post/8424207595/installing-node-js-npm-express-mongoose-on-ubuntu
如果由于某种原因缺少 NPM 命令,请使用npm help. 如果没有,请按照以下步骤操作 - http://arnolog.net/post/8424207595/installing-node-js-npm-express-mongoose-on-ubuntu
If just the Express command is not working, try:
如果只是 Express 命令不起作用,请尝试:
sudo npm install -g express
This made everything work as I'm used to with Windows7 and OSX.
这使得一切正常,就像我习惯使用 Windows7 和 OSX 一样。
Hope this helps!
希望这可以帮助!
回答by Saurav Kumar
You need to have superuser privileges,
你需要有超级用户权限,
sudo npm install -g <package name>
回答by Ratnesh Kushwaha
In Ubuntu, set path of node_modules in .bashrcfile
在 Ubuntu 中,在.bashrc文件中设置 node_modules 的路径
export PATH="/home/username/node_modules/.bin:$PATH"
导出路径="/home/username/node_modules/.bin:$PATH"
回答by Dmitri Bouianov
You might not have write permissions to install a node module in the global location such as /usr/local/lib/node_modules, in which case run npm install -g package as root.
您可能没有在全局位置安装节点模块的写入权限,例如/usr/local/lib/node_modules,在这种情况下,以 root 身份运行 npm install -g package。

