节点module.paths 混乱
node module.paths confusion
节点module.paths混乱
问题:所需的节点模块不使用我的全局 module.paths,即使我在我的 main.js 应用程序中将全局路径添加到数组。
示例:
我的main.js
//global modules path
module.paths.push('C:\Users\xuser\AppData\Roaming\npm');
// finds ws in global modules path. Works!
wsmain=require('ws')
// Now load a 3rd party module, which also requires('ws')
C = require('cmod.js');
cmod.js
ws=require('ws'); // fails to find global path
q:如何确保模块全局也传递给需要的模块。有没有办法将它作为参数或其他东西传递?
我不确定是否存在“全局”路径。 node.js 文档建议默认 require
相对于需要它的文件发生,并在目录链上搜索 node_modules
目录。来自 docs:
For example, if the file at '/home/ry/projects/foo.js' called
require('bar.js'), then node would look in the following locations, in
this order:
- /home/ry/projects/node_modules/bar.js
- /home/ry/node_modules/bar.js
- /home/node_modules/bar.js
- /node_modules/bar.js
看起来你可以使用 NODE_PATH
env var 来给出查找模块的位置列表:
https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
根据文档,以下可能有效:
NODE_PATH=/path/to/node_modules/where/ws/lives node mymain.js
节点module.paths混乱
问题:所需的节点模块不使用我的全局 module.paths,即使我在我的 main.js 应用程序中将全局路径添加到数组。
示例:
我的main.js
//global modules path
module.paths.push('C:\Users\xuser\AppData\Roaming\npm');
// finds ws in global modules path. Works!
wsmain=require('ws')
// Now load a 3rd party module, which also requires('ws')
C = require('cmod.js');
cmod.js
ws=require('ws'); // fails to find global path
q:如何确保模块全局也传递给需要的模块。有没有办法将它作为参数或其他东西传递?
我不确定是否存在“全局”路径。 node.js 文档建议默认 require
相对于需要它的文件发生,并在目录链上搜索 node_modules
目录。来自 docs:
For example, if the file at '/home/ry/projects/foo.js' called require('bar.js'), then node would look in the following locations, in this order:
- /home/ry/projects/node_modules/bar.js
- /home/ry/node_modules/bar.js
- /home/node_modules/bar.js
- /node_modules/bar.js
看起来你可以使用 NODE_PATH
env var 来给出查找模块的位置列表:
https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
根据文档,以下可能有效:
NODE_PATH=/path/to/node_modules/where/ws/lives node mymain.js