问题
I would like that
~ $ npm install express"
will not install express in my home folder, but instead in ~/apps/node_modules.
So I inserted this export NODE_PATH=/home/a/apps/node_modules in .bashrc and than I did source .bashrc.
Unfortunately, npm install express still installs it in /home/a/node_modules and npm install -g express require root/Administrator.
How is it possible that npm install packages in /home/a/apps/node_modules?
回答1:
NODE_PATH is used to find modules, but not to install them.
You can use --prefix to signify where npm should install a package:
npm --prefix ~/apps install express
You could make it more permanent by configuring npm to always use that prefix, but that would only overwrite the global location. So you would need to use the -g flag:
# config once
npm config set prefix ~/apps
# after that, '-g' will install packages in ~/apps
npm install express -g
Also, using -g will install packages in ~/apps/lib/node_modules (which, I think, cannot be changed), so add that directory to your $NODE_PATH as well.
Another solution would be to create a shell alias for npm:
# in your ~/.bashrc
alias npm="command npm --prefix ~/apps"
Although I'm not entirely sure how that would work together with -g.
来源:https://stackoverflow.com/questions/16684908/node-path-no-effect