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! < npm ERR! or email it to: npm ERR! <> 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 1020 Answers
It says
Cannot find module 'express' Do you have express installed? If not then run this.
npm install express and run your program again.
7After 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.
2Check 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% 2npm install --save express
This worked for me. Just run express.js installation again.
0npm install from within your app directory will fix the issue as it will install everything required
0Unless 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
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
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.
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
0for 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
0Sometimes there are error while installing the node modules Try this:
- Delete node_modules
- npm install
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!
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!
1npm 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!!
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/ 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.
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.
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
Have you tried
npm install If you're specifically looking for just express
npm install --save express 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" }