Reputation: 11905
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
Upvotes: 193
Views: 541563
Reputation: 471
npm install
from within your app directory will fix the issue as it will install everything required
Upvotes: 14
Reputation: 440
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.
Upvotes: 0
Reputation: 83
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
Upvotes: 0
Reputation: 39
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.
Upvotes: 0
Reputation: 462
On Debian the easiest way is to issue as root
apt install node-express
Upvotes: 1
Reputation: 560
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" }
Upvotes: 0
Reputation: 302
Sometimes there are error while installing the node modules Try this:
Upvotes: 3
Reputation: 5688
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.
Upvotes: 56
Reputation: 553
npm install --save express
This worked for me. Just run express.js installation again.
Upvotes: 42
Reputation: 3548
It says
Cannot find module 'express'
Do you have express installed? If not then run this.
npm install express
and run your program again.
Upvotes: 263
Reputation: 2324
Have you tried
npm install
If you're specifically looking for just express
npm install --save express
Upvotes: 0
Reputation: 491
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
Upvotes: 0
Reputation: 733
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.
Upvotes: 0
Reputation: 808
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
Upvotes: 8
Reputation: 31
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
Upvotes: 1
Reputation: 3043
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
Upvotes: 3
Reputation: 1059
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!
Upvotes: 1
Reputation: 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.
Upvotes: -1
Reputation: 3689
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%
Upvotes: 40
Reputation: 161
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/
Upvotes: 0
Reputation: 140
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!!
Upvotes: 0
Reputation: 4151
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
Upvotes: 5
Reputation: 3109
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
Upvotes: 3
Reputation: 16518
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.
Upvotes: 3