0

I wrote a small program using npm express. while I am running the program i am getting error like below.(I am new to node.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:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (C:\Users\node\node_modules\app.js:1:77)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
Ramesh
  • 25
  • 1
  • 6
  • Seems like you haven't installed `express` inside your local `node_modules` folder (and neither globally?)... You might check if it's installed with `npm list` inside the directory you're running the script. – ConcurrentHashMap Mar 07 '13 at 08:32
  • I installed npm express C:\Users\node\node_modules>npm express -v 1.2.11 I am able to get version of express using above command.. so it is installed... – Ramesh Mar 07 '13 at 08:35
  • That's the wrong place to install it to. See my answer below for the reasons. And in case it answers your question it would be great if you could a) upvote it, and b) mark it as answer. Thanks :-) – Golo Roden Mar 07 '13 at 08:42

1 Answers1

3

Inside your app you obviously require the express module, probably like this:

var express = require('express');

For this line to work you need to install Express into the local context of your application. To do so run

$ npm install express

inside your application's folder. This will (if it does not yet exist) create a folder node_modules where all your dependencies go.

Additionally, I'd suggest that you put Express into your package.json inside the dependencies block, such as:

"dependencies": {
  "express": "3.1.0"
}

Of course, you can adjust the version number to whatever version you use. Once you've done this for all of your dependencies, you can install them at once by simply running

$ npm install

That should fix it.

PS: It does not matter for this scenario whether you installed Express globally or not. A global installation is only good for having the express bootstrapper available system-wide. The require function always only searches within the local application context.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • +1 for recommending package.json, it'll make the OP's life easier if he ever decides to deploy to paas like heroku or nodejitsu. – booyaa Mar 07 '13 at 08:46
  • Thanks, but it's not only this, it's also co-workers who check out the project from source control (and you definitiely don't want to check in the `node_modules` folder). – Golo Roden Mar 07 '13 at 09:08