Nodejs Error: Cannot find module 当前文件夹下的模块

当执行如下代码是,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 查找模块的路径中

参考文档:https://gist.github.com/branneman/8048520

原文如下:
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.)

你可能感兴趣的:(nodejs)