我已经在 stackoverflow 上阅读了 3-4 个相关主题,但我似乎无法运行我的 Node 。
我尝试运行:
node app.js local
它返回:
Error: Cannot find module './config'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/larsfalcon/Documents/lkrieger/git/testinggrounds/app.js:2:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
在运行此命令之前,我会执行以下操作:
npm i
它会安装我的软件包,我可以在我的node_modules中看到express的存在,但即使我尝试在CLI中运行express,它也说它不是bash,所以我认为它仍然没有以某种方式安装?
这里是对我运行 Node 所需的 2 个 app.js 和 index.js 文件的回顾。
App.js
var config = require('./config')();
http.createServer(app).listen(config.port, function(){
console.log('Express server listening on port ' + config.port);
});
index.js
var config = {
local: {
mode: 'local',
port: 3000
},
staging: {
mode: 'staging',
port: 4000
},
production: {
mode: 'production',
port: 5000
}
}
module.exports = function(mode) {
return config[mode || process.argv[2] || 'local'] || config.local;
}
我应该做什么?
最佳答案
这意味着 app.js
所在的文件夹中没有 config.js
。
更新:你需要require('./config/config')
。
关于javascript - Node.js Express JS - 找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25829362/