节点版本管理器(NVM)npm将模块安装到公用文件夹 [英] Node Version Manager (NVM) npm installing modules to common folder
问题描述
我已经按照这篇文章中的说明为node.js安装了NVM:
I've installed NVM for node.js using the instructions from this post:
http://www.backdrifter.com/2011/02/18/using-nvm-and-npm-to-manage-node-js/
当我在节点版本之间切换,然后使用npm安装模块时,所有模块都放在同一"node_modules"文件夹(〜/node_modules/)中,而不是在特定于该节点版本的"node_modules"目录中?
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?
关于如何解决这个问题的任何想法?
Any idea on how to remedy this?
推荐答案
基于 https中的评论://github.com/creationix/nvm/pull/97 :
使用全局开关-g和npm安装软件包时, 程序包最终位于正确的目录中(即 .nvm/$ VERSION/lib/node_modules),但是节点无法要求它 因为它不以某种方式搜索其前缀.
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.
因此,使用npm install -g xxxxx
会将模块放置在NVM的正确位置,但是如果尝试require
,则其中一个节点将找不到该模块.我仍在处理这个问题,如果找到解决方案,它将进行更新.
So using npm install -g xxxxx
will put the modules in the correct location for NVM but if you try to require
one of them node can't find the module. I am still playing around with this and will update if I find a solution.
更新
NPM将node_modules放在哪里? (请参见 https://docs.npmjs.com/files/folders )
Where does NPM put node_modules? (see https://docs.npmjs.com/files/folders)
- 本地安装(默认):将内容放入当前软件包根目录的./node_modules中.
- 全局安装(带有-g):将内容放入/usr/local或安装节点的任何地方.
- 如果要
require()
,请在本地安装. - 如果要在命令行上运行它,请全局安装它.
- 如果两者都需要,则将其安装在两个地方,或使用
npm link
.
- 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
.
所以我的操作是运行npm init
(请参见 http://npmjs.org/doc/init .html )在我的项目根目录中,该目录生成package.json.现在,当我运行npm install xxxxx
时,它将在我的项目文件夹(将其添加到.gitignore中)中创建一个node_modules目录.这适用于我的代码中需要的模块.
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 xxxxx
it 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.
对于诸如CoffeeScript之类的命令,我使用npm install -g coffee-script
安装,将其放置在正确的目录(.nvm/$ VERSION/lib/node_modules)中.虽然我不需要这些模块(npm链接应该可以解决此问题),但是我可以运行命令-即coffee
.
For commands such as CoffeeScript I install with npm install -g coffee-script
which 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
.
这篇关于节点版本管理器(NVM)npm将模块安装到公用文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!