node.js, Error: Cannot find module 'express'

  • 11,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 11
  • Tình trạng: Còn hàng

I am new to tát Node.js, try to tát learn express to tát build my first trang web application. I got stuck on my very first sample code and need some help to tát get it running. Before I post this question, I did tìm kiếm 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.đôi mươi path to tát node:    /Users/feelexit/nvm/v0.8.20/bin/node
path to tát express: /Users/feelexit/nvm/node_modules/express

here's my sample code: this tệp tin locates at:

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

var express = require('express');
var tiện ích = express();
app.get('/', function(req, res){
    res.send('welcome to tát express');
});
app.listen(3000);

when I try to tát lập cập this command node index.js I get following error message, please help mạ to tát 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. (/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 tát 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 tin nhắn 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