Node.js Error: Cannot find module express

Dmitry picture Dmitry · Jun 18, 2013 · Viewed 307.6k times · Source

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 [email protected] 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 [email protected] No repository field.
npm WARN package.json [email protected] No readme data.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] 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 [email protected] No repository field.
npm http 304 https://registry.npmjs.org/pause/0.0.1
npm WARN package.json [email protected] 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 [email protected] 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
[email protected] C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express
├── [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])

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 [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
C:\Users\Dmitry\AppData\Roaming\npm
└── [email protected]


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

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

Answer

Golo Roden picture Golo Roden · Jun 18, 2013

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.