最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js Express JS - Could not find module - Stack Overflow

programmeradmin6浏览0评论

Ive allready read 3-4 topics on this here at stackoverflow, but i simply cant seem to run my node.

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._pile (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 mand 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?

here is a review of my 2 app.js and index.js files that i need to run my node.

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;
}

What should i do?

Ive allready read 3-4 topics on this here at stackoverflow, but i simply cant seem to run my node.

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._pile (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 mand 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?

here is a review of my 2 app.js and index.js files that i need to run my node.

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;
}

What should i do?

Share Improve this question asked Sep 14, 2014 at 1:46 NopzenNopzen 5762 gold badges6 silver badges20 bronze badges 2
  • Where are your require statements? – Mike Perrenoud Commented Sep 14, 2014 at 1:49
  • In app.js Updatede width require statement. Sorry for that. – Nopzen Commented Sep 14, 2014 at 1:49
Add a ment  | 

1 Answer 1

Reset to default 3

So that means that there is no config.js in the same folder as the app.js.

UPDATE: you'll want require('./config/config').

发布评论

评论列表(0)

  1. 暂无评论