bash Node.js Express JS - 找不到模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25829362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js Express JS - Could not find module
提问by Nopzen
Ive allready read 3-4 topics on this here at stackoverflow, but i simply cant seem to run my node.
我已经在 stackoverflow 上阅读了 3-4 个关于此的主题,但我似乎无法运行我的节点。
I Try to run:
我尝试运行:
node app.js local
and it returns:
它返回:
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)
Before i run this command i do:
在运行此命令之前,我会执行以下操作:
npm i
And it installs my packages, i can physicaly see in my node_modules that express is there, but even if i try run express in the CLI it says its not a bash so i assume its not installed somehow still?
并且它安装了我的软件包,我可以在我的 node_modules 中物理上看到 express 在那里,但是即使我尝试在 CLI 中运行 express,它也说它不是 bash,所以我假设它没有以某种方式安装?
here is a review of my 2 app.js and index.js files that i need to run my node.
这是我运行节点所需的 2 个 app.js 和 index.js 文件的回顾。
App.js
应用程序.js
var config = require('./config')();
http.createServer(app).listen(config.port, function(){
console.log('Express server listening on port ' + config.port);
});
index.js
索引.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;
}
What should i do?
我该怎么办?
回答by Mike Perrenoud
So that means that there is no config.js
in the same folder as the app.js
.
所以这意味着没有config.js
与app.js
.
UPDATE:you'll want require('./config/config')
.
更新:你会想要require('./config/config')
.