当执行如下代码是,nodejs 报错 Cannot find module(a.js 为当前文件夹下的同级文件)
var x = require("a.js")
修改为 ./ 后正常
var x = require("./a.js")
或者执行node命令入口时添加 (index.js 为主入口文件)
NODE_ENV . node index.js
主要目的:将当前文件夹”.” 添加到node 查找模块的路径中
原文如下:
4.2. Only while executing node
This solution will not affect your environment other than what node preceives. It does change your application start command.
Start your application like this from now on:
Linux: NODE_PATH=. node app
Windows: cmd.exe /C “set NODE_PATH=.&& node app”
(On Windows this command will not work if you put a space in between the path and the &&. Crazy shit.)
本文介绍了在Node.js中遇到的模块加载错误Cannot find module,并提供了解决方案,包括正确使用相对路径和设置NODE_PATH环境变量的方法。

3万+

被折叠的 条评论
为什么被折叠?



