163

I am new to Node.js, try to learn express to build my first web application. I got stuck on my very first sample code and need some help to get it running. Before I post this question, I did search on stack overflow, found some similar questions but still could not fix it.

Error: Cannot find module 'express'

I am using mac os 10.8.2. I have Node.js installed using nvm.

node.js: 0.8.20 path to node:    /Users/feelexit/nvm/v0.8.20/bin/node
path to express: /Users/feelexit/nvm/node_modules/express

here's my sample code: this file locates at:

/Users/feelexit/WebstormProjects/learnnode/node_modules/index.js

var express = require('express');
var app = express();
app.get('/', function(req, res){
    res.send('welcome to express');
});
app.listen(3000);

when I try to run this command node index.js I get following error message, please help me to fix it.

Thank you.

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> (/Users/feelexit/WebstormProjects/learnnode/node_modules/index.js:1:81)
    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)
feelexits-Mac:node_modules feelexit$ 

Update to answer chovy's question:

feelexits-Mac:~ feelexit$ npm install
npm ERR! install Couldn't read dependencies
npm ERR! Error: ENOENT, open '/Users/feelexit/package.json'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Darwin 12.2.0
npm ERR! command "/Users/feelexit/nvm/v0.8.20/bin/node" "/Users/feelexit/nvm/v0.8.20/bin/npm" "install"
npm ERR! cwd /Users/feelexit
npm ERR! node -v v0.8.20
npm ERR! npm -v 1.2.11
npm ERR! path /Users/feelexit/package.json
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/feelexit/npm-debug.log
npm ERR! not ok code 0
Martin Brisiak
  • 3,473
  • 12
  • 35
  • 49
qinking126
  • 10,525
  • 21
  • 70
  • 117
  • May be this will help http://stackoverflow.com/questions/9133784/node-version-manager-nvm-npm-installing-modules-to-common-folder – user568109 Feb 19 '13 at 03:26
  • 38
    Did you type `npm install` – chovy Feb 19 '13 at 03:32
  • 1
    Are you certain that the express library is in your "node_modules" folder? You may also want to try moving your _index.js_ file up one directory to "/Users/feelexit/WebstormProjects/learnnode/" and leave the node_modules folder alone. – Default Feb 19 '13 at 03:53
  • 1
    Please post your `package.json` file. That will help in debugging the problem. – Akhil Raina Feb 19 '13 at 07:27
  • @chovy, I use npm install express to install express. what does only "npm install" do ? – qinking126 Feb 19 '13 at 12:12
  • @Akhil Raina, where is the package.json file? – qinking126 Feb 19 '13 at 12:21
  • @Default, yes, it is in the node_modules folder. I moved it one direotry up to "/Users/feelexit/WebstormProjects/learnnode/", still same error. – qinking126 Feb 19 '13 at 12:24
  • Does your express library have a _package.json_ file in the express/ directory (express/package.json)? Also, I just noticed that you have your express module located in a different directory than your project. That is probably the problem since you are trying to `require()` it locally. Move your express module from _/Users/feelexit/nvm/node_modules/express_ to _/Users/feelexit/WebstormProjects/learnnode/node_modules/express_ and let me know if that solves your problem. [This info](https://npmjs.org/doc/folders.html) can give you more detail about node_module file structures. – Default Feb 19 '13 at 15:13
  • possible duplicate of [Node.js Error: Cannot find module express](http://stackoverflow.com/questions/17162308/node-js-error-cannot-find-module-express) – Evan Carroll Nov 11 '13 at 06:20
  • Make sure your `package.json` file has all the required libraries before `npm install` I had installed them via the command line previously without the `--save` and they were not in the `package.json` file, so basically my setup was missing dependencies. –  Dec 08 '20 at 19:41

20 Answers20

218

It says

Cannot find module 'express'

Do you have express installed? If not then run this.

npm install express

and run your program again.

Tiago Martins Peres
  • 12,598
  • 15
  • 77
  • 116
Saurabh Rana
  • 3,022
  • 1
  • 17
  • 20
  • 2
    This does not fix the problem in my case. `npm install express` shows this error `npm ERR! Error: UNKNOWN, symlink '../express/bin/express'`. Seems to be a deeper issue. – Sliq Oct 15 '13 at 00:36
  • Use sudo if you have not fixed your directory permissions for npm. In any case, "sudo npm install" is a better option. – Gaurav Gupta Dec 10 '14 at 08:04
  • symlink Errors often occur, if you installed things globally and, yeah, the symlink broke. Try `npm link` to re-create the respective linking... – Frank Nocke May 17 '16 at 11:44
  • @Sliq I tried with your solution but i can't solve my problem can you help me ? – VjyV Oct 10 '16 at 06:47
  • Don't overlook "And run your program again." In my case i had to restart the process in my `process manager`. It didn't restart by itself. – Tadej May 08 '17 at 07:40
  • Prolly too late! But you shall redo npm install, it will fetch all the missing dependency at once. – Saurabh Rana Nov 17 '20 at 03:30
  • But surely one should be able to run a bundle without dependencies post build, no? – iMe May 29 '22 at 20:03
50

After you do express in your terminal, then do

npm install

To install all the dependencies.

Then you can do node app to run the server.

Tiago Martins Peres
  • 12,598
  • 15
  • 77
  • 116
Bill
  • 5,210
  • 14
  • 59
  • 93
  • 8
    You should point out that this requires a package list file ie .json.. And this is not always the case. – Pogrindis Nov 07 '13 at 11:11
  • @VjyV in the same directory you have your .js node file. But it should have the package.json file in the directory so that it can install the required dependencies. – Saurabh Rana Dec 20 '16 at 05:41
39

Check if you have installed express module. If not, 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,478
  • 1
  • 26
  • 40
  • 15
    **This** is the right answer, to everyone posting these `CTRL+C / CTRL+V` `npm install` answers: do you really think anyone would try to run something without checking if its installed first? Seriously... – Claudio Holanda Jun 14 '14 at 21:45
  • 4
    Just adding a minor detail: You need to run the install express command in the directory of your application and **not** in your Node.js installation folder or the npm-folder in AppData. Because that's the mistake I made, because I thought it was some kind of SDK-add-on. – Alex Dec 10 '14 at 12:34
36
npm install --save express

This worked for me. Just run express.js installation again.

Tiago Martins Peres
  • 12,598
  • 15
  • 77
  • 116
Bonface Ochieng
  • 493
  • 4
  • 4
13

npm install from within your app directory will fix the issue as it will install everything required

Eldad
  • 421
  • 4
  • 3
7

Unless you set Node_PATH, the only other option is to install express in the app directory, like npm install express --save. Express may already be installed but node cannot find it for some reason

Thomas Fritsch
  • 8,893
  • 33
  • 34
  • 46
Shemogumbe
  • 698
  • 6
  • 8
  • 1
    This one worked for me, better execute the install command in same directory as your .js files are. Also you may run command " npm init " to create package.json file to avoid further problems. – emarshah Dec 30 '19 at 23:03
5

Digging up an old thread here BUT I had this same error and I resolved by navigating to the directory my NodeApp resides in and running npm install -d

Wjdavis5
  • 3,736
  • 7
  • 34
  • 60
3

You have your express module located in a different directory than your project. That is probably the problem since you are trying to require() it locally. Try moving your express module from /Users/feelexit/nvm/node_modules/express to /Users/feelexit/WebstormProjects/learnnode/node_modules/express. This info can give you more detail about node_module file structures.

Default
  • 14,844
  • 3
  • 23
  • 36
3

if youre main file is located at /Users/feelexit/WebstormProjects/learnnode/node_modules/index.js then express needs to be located at /Users/feelexit/WebstormProjects/learnnode/node_modules/node_modules as node always looks for modules in ./node_modules (and its internal folder) when the path dont start with ./ or / (more info here)

i think you miss placed youre main file in the module folder

VeXii
  • 2,979
  • 1
  • 18
  • 25
3

for this scenario run npm install express command using your cmd prompt for the respective folder where you want to run the program. Example I want to run the express module program server.js in F:\nodeSample. So run "npm install express" in that particular folder then run server.js

Rijo
  • 2,761
  • 3
  • 26
  • 52
2

Sometimes there are error while installing the node modules Try this:

  1. Delete node_modules
  2. npm install
1

Run npm install express body-parser cookie-parser multer --save command in the same directory with your source code nodejs file to resolve this issue. P/s: check your directory after run command to understand more!

Do Tat Hoan
  • 703
  • 7
  • 9
1

In rare cases, npm cache may get corrupt. For me, what worked was:

npm cache clean --force

Generally, the package manager will detect corruption and refetch on its own so this is not usually necessary. However, in my case Windows 10 crashed a few times and I suspect this may have been during a fetch operation. Hope it helps someone!

More information: https://docs.npmjs.com/cli/cache

jwerner
  • 31
  • 3
  • Thanks! In my case node get corrupt after installing BigSur. Cache clean worked! – MoD Feb 28 '21 at 22:08
0

npm ERR! Error: ENOENT, open '/Users/feelexit/package.json'

This happens due to missing permissions or unlinked files while npm was working.

Meaning, that executing npm as this user doesn't have enough rights to read/write from a file, in this case package.json.

try adding sudo before the entire command - it should resolve.

$ sudo npm install -g express
$ Password:*******

Password would be your admin password of your mac.

-g flag will install this module (express) in the global context of node - meaning node will/should recognize express module from within any js file without having to provide a full path to the module in use.

Hope this helps!!

obfuscate
  • 130
  • 7
0

I had the same problem. My issue was that I have to change to the Node.js project directory on the command line before installing express.

cd /Users/feelexit/WebstormProjects/learnnode/node_modules/
user3377708
  • 121
  • 6
  • 16
0

I'm guessing that this is coursework from Colt Steele's Web Development course... I was looking for the same answer as to why I ended up with that error too.. Colt doesn't say so but you take the node_module folder and move into the new folder you're working in... that's what worked for me.

0
D:\learn\Node.js\node app.js
module.js:549
    throw err;
    ^

Error: Cannot find module 'body-parser'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

Sometimes version not match with package.json Fixed the problem by checking the package.json then use the following commands: npm install body-parser@1.13.2 it resolved for me.

Sarath Kumar
  • 685
  • 1
  • 8
  • 11
0

I've came across a similar problem and in the end it was a matter of some old dependencies that were messing up my Heroku server.

While at my project's folder I've run:

npm uninstall
npm install

I hope it helps

HClx
  • 461
  • 4
  • 5
0

Have you tried

npm install

If you're specifically looking for just express

npm install --save express
Akash Yellappa
  • 1,760
  • 23
  • 19
0

In my case I was trying to run the same as you but using nodemon. The error was the same but the problem was because on my package.json I added app.js instead of just app

"script" : { "dev": "nodemon app" }

Yorsh
  • 490
  • 7
  • 9