Node js express is installed however still throwing not found error

Issue

i have installed express using:

npm install express -g

Now when i call:

npm install express

i get the following result:

    /home/marc/node
└── [email protected] 

So when i want to run my hello world script:

    var express = require('express');
var app = express();
app.get('/', function(req, res){
    res.send('Hello World');
});

app.listen(3000);

i still get the following error:

    Marc node # node /home/marc/nodejstest/server.js

module.js:340
    throw err;
          ^
Error: Cannot find module 'express'
    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> (/home/marc/nodejstest/server.js:8:15)
    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)

Can anyone tell me what might be the problem here?

my npm list:

    Marc node # npm list
/home/marc/node
└─┬ [email protected]
  ├─┬ [email protected]
  │ ├─┬ [email protected]
  │ │ └── [email protected]
  │ └── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ └── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ └── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ └── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ ├── [email protected]
  │ └── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ ├── [email protected]
  │ ├── [email protected]
  │ └── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ └─┬ [email protected]
  │   └── [email protected]
  ├── [email protected]
  └── [email protected]

Solution

As discussed in chat, you will want to install express local instead of global.

Local install and save to package.json:

npm install express --save

Express Docs:

http://expressjs.com/

Also Note: the following command will help you determine which packages are globally installed. Global installs are okay, but sometimes there is some extra ground work needed. (IE, adding appropriate paths to your system paths)

To see global packages:

npm list -g --depth=0

Global vs Local:

Some extra reading on global vs local.

https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/

Answered By – khollenbeck

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published