node.js 节点版本管理器 (NVM) npm 将模块安装到公共文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9133784/
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
Node Version Manager (NVM) npm installing modules to common folder
提问by grookle
I've installed NVM for node.js using the instructions from this post:
我已经使用这篇文章中的说明为 node.js 安装了 NVM:
http://www.backdrifter.com/2011/02/18/using-nvm-and-npm-to-manage-node-js/
http://www.backdrifter.com/2011/02/18/using-nvm-and-npm-to-manage-node-js/
When I switch between node versions and then use npm to install a module, all the modules are placed in the same 'node_modules' folder (~/node_modules/) instead of in the 'node_modules' directory specific to that version of node?
当我在节点版本之间切换然后使用 npm 安装模块时,所有模块都放置在同一个“node_modules”文件夹(~/node_modules/)中,而不是特定于该版本节点的“node_modules”目录中?
Any idea on how to remedy this?
关于如何解决这个问题的任何想法?
回答by Jesse Vogt
Based on the comments from https://github.com/creationix/nvm/pull/97:
基于来自https://github.com/creationix/nvm/pull/97的评论:
When installing packages with npm using the global switch -g the package ends up in the proper directory (i.e. .nvm/$VERSION/lib/node_modules), however node is unable to require it since it somehow isn't searching on it's prefix.
当使用全局开关 -g 使用 npm 安装包时,包最终位于正确的目录中(即 .nvm/$VERSION/lib/node_modules),但是 node 无法要求它,因为它不知何故没有搜索它的前缀。
So using npm install -g xxxxxwill put the modules in the correct location for NVM but if you try to requireone of them node can't find the module. I am still playing around with this and will update if I find a solution.
因此,使用npm install -g xxxxx会将模块放在 NVM 的正确位置,但如果您尝试使用require其中之一,节点将找不到该模块。我仍在玩这个,如果我找到解决方案,我会更新。
Update
更新
Where does NPM put node_modules? (see https://docs.npmjs.com/files/folders)
NPM 把 node_modules 放在哪里?(见https://docs.npmjs.com/files/folders)
- Local install (default): puts stuff in ./node_modules of the current package root.
- Global install (with -g): puts stuff in /usr/local or wherever node is installed.
- Install it locally if you're going to
require()it. - Install it globally if you're going to run it on the command line.
- If you need both, then install it in both places, or use
npm link.
- 本地安装(默认):将东西放在当前包根目录的 ./node_modules 中。
- 全局安装(使用 -g):将东西放在 /usr/local 或安装节点的任何地方。
- 如果您要使用
require()它,请在本地安装它。 - 如果要在命令行上运行它,请全局安装它。
- 如果两者都需要,则在两个地方都安装它,或者使用
npm link.
So what I did was run npm init(see http://npmjs.org/doc/init.html) in my projects root dir which generated package.json. Now when I run npm install xxxxxit creates a node_modules dir in my project folder (which I add to my .gitignore). This works for modules that I require in my code.
所以我所做的是在我的项目根目录中运行npm init(参见http://npmjs.org/doc/init.html),它生成了 package.json。现在,当我运行npm install xxxxx它时,它会在我的项目文件夹中创建一个 node_modules 目录(我将其添加到我的 .gitignore 中)。这适用于我在代码中需要的模块。
For commands such as CoffeeScript I install with npm install -g coffee-scriptwhich puts it in the correct directory (.nvm/$VERSION/lib/node_modules). While I can't require these modules (npm link should solve this problem) I can run the commands - i.e. coffee.
对于诸如 CoffeeScript 之类的命令,我安装时npm install -g coffee-script会将其放在正确的目录 (.nvm/$VERSION/lib/node_modules) 中。虽然我不能要求这些模块(npm link 应该可以解决这个问题),但我可以运行命令 - 即coffee.
回答by Gisael Gustavo Gómez González
I just installed express globally (-g) and was having problem when require("express"). Just like Jesse Vogt said I just reinstalled express but this time without the -g just like this: "sudo npm install express" and now is working perfectly!
我刚刚全局安装了 express (-g) 并且在 require("express") 时遇到了问题。就像 Jesse Vogt 说的那样,我刚刚重新安装了 express,但这次没有 -g 就像这样:“sudo npm install express”,现在运行良好!

