问题
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