我意识到,当我全局安装 node.js 模块(带有 -g 标志)时,除非我编写了整个路径,否则 Node 无法使用该模块。
我的意思是,如果模块已全局安装,这将不起作用:
cheerio = require('cheerio'),
我必须这样写:
cheerio = require('/usr/lib/node_modules/cheerio'),
我如何告诉 Node 它必须在正确的路径中寻找模块?
谢谢。
最佳答案
一般来说,我建议让 npm 为您提供路径并如上所述设置:
$ echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bash_profile && . ~/.bash_profile
关于node.js 模块路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13465829/