171

I wrote my first node.js app, but it can't find express library:

C:\ChatServer\Server>node 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> (C:\ChatServer\Server\server.js:6:9)
    at Object.<anonymous> (C:\ChatServer\Server\server.js:25:4)
    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)

But express is intalled (with -g key):

C:\ChatServer\Server>npm install -g express
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/connect/2.7.11
npm http GET https://registry.npmjs.org/commander/0.6.1
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/mkdirp/0.3.4
npm http GET https://registry.npmjs.org/cookie/0.1.0
npm http GET https://registry.npmjs.org/buffer-crc32/0.2.1
npm http GET https://registry.npmjs.org/fresh/0.1.0
npm http GET https://registry.npmjs.org/methods/0.0.1
npm http GET https://registry.npmjs.org/send/0.1.0
npm http GET https://registry.npmjs.org/cookie-signature/1.0.1
npm http GET https://registry.npmjs.org/debug
npm http 304 https://registry.npmjs.org/mkdirp/0.3.4
npm http 304 https://registry.npmjs.org/range-parser/0.0.4
npm http 304 https://registry.npmjs.org/cookie/0.1.0
npm http 304 https://registry.npmjs.org/connect/2.7.11
npm http 304 https://registry.npmjs.org/commander/0.6.1
npm WARN package.json range-parser@0.0.4 No repository field.
npm http 304 https://registry.npmjs.org/buffer-crc32/0.2.1
npm http 304 https://registry.npmjs.org/fresh/0.1.0
npm http 304 https://registry.npmjs.org/methods/0.0.1
npm http 304 https://registry.npmjs.org/send/0.1.0
npm http 304 https://registry.npmjs.org/cookie-signature/1.0.1
npm WARN package.json methods@0.0.1 No repository field.
npm WARN package.json methods@0.0.1 No readme data.
npm WARN package.json fresh@0.1.0 No repository field.
npm WARN package.json send@0.1.0 No repository field.
npm WARN package.json cookie-signature@1.0.1 No repository field.
npm http 304 https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/mime/1.2.6
npm http GET https://registry.npmjs.org/qs/0.6.5
npm http GET https://registry.npmjs.org/send/0.1.1
npm http GET https://registry.npmjs.org/formidable/1.0.14
npm http GET https://registry.npmjs.org/cookie/0.0.5
npm http GET https://registry.npmjs.org/bytes/0.2.0
npm http GET https://registry.npmjs.org/pause/0.0.1
npm http 304 https://registry.npmjs.org/mime/1.2.6
npm http 304 https://registry.npmjs.org/bytes/0.2.0
npm WARN package.json bytes@0.2.0 No repository field.
npm http 304 https://registry.npmjs.org/pause/0.0.1
npm WARN package.json pause@0.0.1 No repository field.
npm http 304 https://registry.npmjs.org/formidable/1.0.14
npm http 304 https://registry.npmjs.org/qs/0.6.5
npm http 304 https://registry.npmjs.org/send/0.1.1
npm http 304 https://registry.npmjs.org/cookie/0.0.5
npm http GET https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/mime
C:\Users\Dmitry\AppData\Roaming\npm\express -> C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express\bin\express
npm WARN package.json policyfile@0.0.4 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
npm WARN package.json assert-plus@0.1.2 No repository field.
npm WARN package.json ctype@0.5.2 No repository field.
express@3.2.6 C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express
├── methods@0.0.1
├── fresh@0.1.0
├── range-parser@0.0.4
├── cookie-signature@1.0.1
├── buffer-crc32@0.2.1
├── cookie@0.1.0
├── debug@0.7.2
├── commander@0.6.1
├── mkdirp@0.3.4
├── send@0.1.0 (mime@1.2.6)
└── connect@2.7.11 (pause@0.0.1, qs@0.6.5, bytes@0.2.0, cookie@0.0.5, formidable@1.0.14, send@0.1.1)

Some information:

C:\ChatServer\Server>npm --version
1.2.24

C:\ChatServer\Server>node --version
v0.10.9

C:\ChatServer\Server>npm --version
1.2.24

C:\ChatServer\Server>npm ls -g installed express
npm WARN package.json cookie-signature@1.0.1 No repository field.
npm WARN package.json fresh@0.1.0 No repository field.
npm WARN package.json methods@0.0.1 No repository field.
npm WARN package.json range-parser@0.0.4 No repository field.
npm WARN package.json send@0.1.0 No repository field.
npm WARN package.json policyfile@0.0.4 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
npm WARN package.json bytes@0.2.0 No repository field.
npm WARN package.json pause@0.0.1 No repository field.
npm WARN package.json assert-plus@0.1.2 No repository field.
npm WARN package.json ctype@0.5.2 No repository field.
C:\Users\Dmitry\AppData\Roaming\npm
└── express@3.2.6


C:\ChatServer\Server>npm ls installed express
C:\ChatServer\Server
└── (empty)

How can I solve this problem? (MUST I install it without -g?)

Dmitry
  • 7,457
  • 12
  • 57
  • 83
  • 2
    possible duplicate of [NodeJS - Unable to import global modules](http://stackoverflow.com/questions/7970793/nodejs-unable-to-import-global-modules) – Evan Carroll Nov 11 '13 at 06:46
  • possible duplicate of [NodeJS - How to resolve "Cannot find module" error](http://stackoverflow.com/questions/9023672/nodejs-how-to-resolve-cannot-find-module-error) – BuZZ-dEE Sep 23 '15 at 21:30
  • 1
    Possible duplicate of [node.js, Error: Cannot find module 'express'](https://stackoverflow.com/questions/14949118/node-js-error-cannot-find-module-express) – Thusila Bandara Oct 13 '17 at 09:26

15 Answers15

318

You need to install Express locally into the context of your application (node_modules folder):

$ npm install express

The reason for this is that applications always look in their local context for any dependencies. The global installation is only for setting up system-wide available binaries, such as unit test runners or bootstrappers or things like that.

With Express, when you install it globally, you get an express binary that can bootstrap an application for you. For more information, type

$ express --help

So, to answer your final question: YES, you need to install it without -g.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • 1
    This helped me too. I had `winston` installed globally, which worked for some reason when starting my app as a normal user, but when starting my app and a system init script it wouldn't work. I fixed it by installing winston locally in the app directory (and adding it to package.json). – trusktr Apr 21 '14 at 02:08
  • 4
    you can link a global installed dependency by cd to your project root directory and then use "npm link " – khoi nguyen Sep 29 '14 at 14:34
  • 14
    **npm install** after you've created your app. It'll install all dependencies. –  Mar 24 '15 at 08:44
  • Thanks this really helped. Going to node_modules folder and than installing express solved my problem – Megha Jul 31 '16 at 09:17
  • @Golo Roden Accoding your answer i tried with your solution but it's throwing error : ' npm ERR! tar.unpack untar error C:\Users\VV\AppData\Roaming\npm-cache \express\4.14.0\package.tgz npm ERR! Windows_NT 6.3.9600 npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs \\node_modules\\npm\\bin\\npm-cli.js" "install" "express" – VjyV Oct 10 '16 at 09:40
  • in project directory **rm -rf node_modules/** then **npm install** – mvladk Apr 06 '17 at 09:24
  • and then install all the libraries you need to install – Pavlos Oct 11 '19 at 10:39
23

For me it worked when installed express locally with --save option as follow:

$ npm install express --save
Mustafamg
  • 714
  • 5
  • 19
13

Check if you are not install express module, use this command:

 npm install express

and if your node_modules directory is in another place, set NODE_PATH envirnment variable:

 set NODE_PATH=your\directory\to\node_modules;%NODE_PATH%
Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41
  • In Windows 10 after the npm install express, it cannot automatically added to node path, you need to manually add the node modules to it. – Jane Aug 25 '17 at 09:25
5

Golo have explain well the solution, but I might add a clarification:
sometimes node modules are installed in

/usr/local/lib/node_modules

and when you launch node blabla.js modules are searched in

/lib

So a solution is to create a symbolic link:

sudo ln -s /usr/local/lib/node_modules/ /lib/node_modules
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
5

Given you have installed node on your system, install Express locally for your project using the following for Windows:

npm install express

or

npm install express --save

You might give it global access by using:

npm install -g express --save
Krishna Ganeriwal
  • 1,903
  • 19
  • 17
4

In your case your express module is installed at C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express, but you need to get this module in to your project directory. So you should copy the file the express module folders from C:\Users\Dmitry\AppData\Roaming\npm\node_modules\ to your project directory as : C:\ChatServer\Server\node_modules. If you do not have a folder named 'node_modules' in your project folder, then create it first and paste those files into this folder. This method worked for me on my windows pc. Restart your node server and once again run the command node C:\ChatServer\Server>node server.js. It should work now !!!!

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
3

On Ubuntu-based OS you can try

sudo apt-get install node-express

its working for me on Mint

Nickolay Savchenko
  • 1,474
  • 16
  • 28
3

I had this error in vscode, although the modules where installed. I am using typescript and express. In the server.ts files all the imports had red squiggly underlines. It turns out I had a faulty tsconfig.json file.

{
    "compileOnSave": false,
    "compilerOptions": {
        "module": "commonjs", // Previously this value was `es6`
        "target": "es6",
        "allowSyntheticDefaultImports": true,
        "baseUrl": "public",
        "sourceMap": true,
        "outDir": "dist",
        "jsx": "react",
        "strict": true,
        "preserveConstEnums": true,
        "removeComments": true,
        "noImplicitAny": true,
        "allowJs": true
    },
    "exclude": [
        "node_modules",
        "build"
    ]
}
Adrian Moisa
  • 3,923
  • 7
  • 41
  • 66
2

I had the same error following the example on this book: "Kubernetes Up & Running".
I see many answers suggesting to install express "by hand" but I'm not convinced is the best solution.
Because we are using package.json (I can see it in the logs) and the right way to build the app is running npm install, I added the express dependency in the package.json file.

 "dependencies": {
    "express": "^4.17.1"
}

I get the current version with npm search express.

Alex 75
  • 2,798
  • 1
  • 31
  • 48
1

create one folder in your harddisk e.g sample1 and go to command prompt type :cd and gives the path of sample1 folder and then install all modules...

npm install express

npm install jade

npm install socket.io

and then whatever you are creating application save in sample1 folder

try it...

Community
  • 1
  • 1
Dev
  • 27
  • 2
1

go to your application directory and install the express module using the below command npm install express --save then list the all install module using the below command npm ls you will see all the locally install modules.

0

I hit the same problem. I had express installed globally at /usr/local/bin/. When I do 'npm install', express was not created in node_modules of local directory.

  1. Check if you have file name .npmrc in your $HOME
  2. If it has 'global = true', change to 'global = false'
  3. Now do 'npm install' in application directory. More likely, you should get all package dependent modules installed in node_modules (local) within application directory.
0
  • sudo brew uninstall node
  • brew update
  • brew upgrade
  • brew cleanup
  • brew install node
  • sudo chown -R $(whoami) /usr/local
  • brew link --overwrite node
  • sudo brew postinstall node

This worked for me on MacOS X Sierra

Durul Dalkanat
  • 7,266
  • 4
  • 35
  • 36
0

I'm not proud sharing this, but in my case I had:

 require('express.handlebars')

 //and the correct form is:
 require('express-handlebars'); //Use dash instead.
David Castro
  • 1,773
  • 21
  • 21
0

1.first check if express is install at correct location. 2. npm install express (run this command). 3. express will save under your "node_modules" folder

Divesh singh
  • 409
  • 4
  • 12