NODE_PATH has no effect on module.paths or finding modules

。_饼干妹妹 提交于 2021-01-06 04:08:14

问题


I'm trying to get node to recognize my grunt node module that is installed in a non-typical directory for a build environment, but updating NODE_PATH is having no effect:

$ env | grep NODE
NODE_PATH=/home/colbblai/.nvm/v0.10.38/lib/node_modules
$ export NODE_PATH=$NODE_PATH:/foo
$ env | grep NODE
NODE_PATH=/home/colbblai/.nvm/v0.10.38/lib/node_modules:/foo
$ which node
/home/colbblai/.nvm/v0.10.38/bin/node
$ node
> module.paths
[ '/mnt/colbblailx.old/home/colbblai/src/repl/node_modules',
  '/mnt/colbblailx.old/home/colbblai/src/node_modules',
  '/mnt/colbblailx.old/home/colbblai/node_modules',
  '/mnt/colbblailx.old/home/node_modules',
  '/mnt/colbblailx.old/node_modules',
  '/mnt/node_modules',
  '/node_modules' ]
> 

From adding /foo to NODE_PATH, I'd expect it in module.paths. What gives? What is the correct way of adding node_modules directories from the command line?


回答1:


On Unix (not on Windows), if the node binary should be treated securely, certain entries stemming from unsecure environment variables are not included in the module.paths array.

More precisely, if the node binary has set-user-ID or set-group-ID or has capabilities, the entries stemming from the environment variables HOME and NODE_PATH will not be included in the module.paths array. These are the following three paths:

$NODE_MODULES
$HOME/.node_modules
$HOME/.node_libraries

So you either can put the modules found in foo somewhere in /mnt/colbblailx.old/home/colbblai/src/node_modules (or similar) or you switch to a node binary not running with neither setuid nor capabilities.

See my answer to this question for some more details.



来源:https://stackoverflow.com/questions/32616989/node-path-has-no-effect-on-module-paths-or-finding-modules

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