I am new to node.js, try to learn express to build my first web application. got stuck on my very first sample code. 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 file "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
It says
Cannot find module 'express'
Do you have express installed?If not then run this.
npm install express
And run your program again.
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.
Check if you are not install express module, 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%
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
npm install from within your app directory will fix the issue as it will install everything required
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
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!!
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/