196

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!     <[email protected]>

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
10
  • 2
    May be this will help stackoverflow.com/questions/9133784/… Commented Feb 19, 2013 at 3:26
  • 42
    Did you type npm install Commented Feb 19, 2013 at 3: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. Commented Feb 19, 2013 at 3:53
  • 1
    Please post your package.json file. That will help in debugging the problem. Commented Feb 19, 2013 at 7:27
  • 1
    @chovy, I use npm install express to install express. what does only "npm install" do ? Commented Feb 19, 2013 at 12:12

24 Answers 24

267

It says

Cannot find module 'express'

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

npm install express

and run your program again.

Sign up to request clarification or add additional context in comments.

7 Comments

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.
Use sudo if you have not fixed your directory permissions for npm. In any case, "sudo npm install" is a better option.
symlink Errors often occur, if you installed things globally and, yeah, the symlink broke. Try npm link to re-create the respective linking...
@Sliq I tried with your solution but i can't solve my problem can you help me ?
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.
|
57

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.

2 Comments

You should point out that this requires a package list file ie .json.. And this is not always the case.
@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.
42
npm install --save express

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

Comments

40

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%

2 Comments

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...
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.
14

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

Comments

8

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

1 Comment

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.
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

2 Comments

Not sure why or how it matters but this installs it in your devdependencies: docs.npmjs.com/cli/install
@GuyLowe -d is debug for npm, -D is dev dependencies.
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.

Comments

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

Comments

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

Comments

3

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

  1. Delete node_modules
  2. npm install

Comments

2

On Debian the easiest way is to issue as root

apt install node-express

1 Comment

fwiw: after trying several of the approaches above, this is the one that finally worked for me
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!

Comments

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

1 Comment

Thanks! In my case node get corrupt after installing BigSur. Cache clean worked!
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!!

Comments

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/

Comments

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 [email protected] it resolved for me.

Comments

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

Comments

0

Have you tried

npm install

If you're specifically looking for just express

npm install --save express

Comments

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" }

Comments

0

This happens when there is an existing image that the user needs to upgrade. Adding a volume in the statement that will create the new container is not enough. That is because what is in the local will overwrite evearything in the new container being created. It is not necessarily desireable to add express/node_nodules to the local.

The easy solution is to add a second, anonymous, volume to the statement that will create the new container and have it indicate which directory in the container is to be preserved.

Comments

0

if

npm install express 

gives error in that case edit package.json file and add following line in it

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

after that run command

npm install

hope that solves the problem

Comments

0

I had this error when trying to ng serve an angular application. Since this application didn't depend on the express package at any point, I cloud solve the problem by deleting the node_modules folder aswell as the package-lock.json file.

Comments

-1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.