Node.js Express JS - Could not find module

16,716

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

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

Share:
16,716
Nopzen
Author by

Nopzen

Software Engineer based out of Copenhagen, Denmark. 8 Years experience with Javascript, both Client & Backend side, mostly worked with React JS, Node JS and Graphql In my spare time I'm dabbling with: Go lang

Updated on June 04, 2022

Comments

  • Nopzen
    Nopzen almost 2 years

    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._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?

    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?

  • Nopzen
    Nopzen over 9 years
    Could you try update the post, i forgot to add the require in app.js
  • Mike Perrenoud
    Mike Perrenoud over 9 years
    @Nopzen where is your config.js located?
  • Nopzen
    Nopzen over 9 years
    In the /config next to the app.js file And its called index.js
  • Nopzen
    Nopzen over 9 years
    Still runs same error, renamede my index.js file (config file) to config.js and did this: var config = require('./config/config.js')(); http.createServer(app).listen(config.port, function(){ console.log('Express server listening on port ' + config.port); });
  • Mike Perrenoud
    Mike Perrenoud over 9 years
    @Nopzen you said the config.js is in a config folder right?
  • Nopzen
    Nopzen over 9 years
    My folder structure is like this index.html app.js config - config.js
  • Mike Perrenoud
    Mike Perrenoud over 9 years
    @Nopzen please see my edit; I didn't mean to include the extension.
  • Nopzen
    Nopzen over 9 years