NODE_PATH no effect

家住魔仙堡 提交于 2021-02-11 01:11:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!