安装了节点js express但是仍然抛出未找到的错误

时间:2015-01-27 14:07:05

标签: node.js express

我使用以下方式安装了express

npm install express -g

现在我打电话:

npm install express

我得到以下结果:

    /home/marc/node
└── express@4.11.1 

所以当我想运行我的hello world脚本时:

    var express = require('express');
var app = express();
app.get('/', function(req, res){
    res.send('Hello World');
});

app.listen(3000);

我仍然收到以下错误:

    Marc node # node /home/marc/nodejstest/server.js

module.js:340
    throw err;
          ^
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:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/marc/nodejstest/server.js:8:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

有谁能告诉我这里可能出现什么问题?

我的npm列表:

    Marc node # npm list
/home/marc/node
└─┬ express@4.11.1
  ├─┬ accepts@1.2.2
  │ ├─┬ mime-types@2.0.7
  │ │ └── mime-db@1.5.0
  │ └── negotiator@0.5.0
  ├── content-disposition@0.5.0
  ├── cookie@0.1.2
  ├── cookie-signature@1.0.5
  ├─┬ debug@2.1.1
  │ └── ms@0.6.2
  ├── depd@1.0.0
  ├── escape-html@1.0.1
  ├─┬ etag@1.5.1
  │ └── crc@3.2.1
  ├── finalhandler@0.3.3
  ├── fresh@0.2.4
  ├── media-typer@0.3.0
  ├── merge-descriptors@0.0.2
  ├── methods@1.1.1
  ├─┬ on-finished@2.2.0
  │ └── ee-first@1.1.0
  ├── parseurl@1.3.0
  ├── path-to-regexp@0.1.3
  ├─┬ proxy-addr@1.0.5
  │ ├── forwarded@0.1.0
  │ └── ipaddr.js@0.1.6
  ├── qs@2.3.3
  ├── range-parser@1.0.2
  ├─┬ send@0.11.1
  │ ├── destroy@1.0.3
  │ ├── mime@1.2.11
  │ └── ms@0.7.0
  ├── serve-static@1.8.1
  ├─┬ type-is@1.5.5
  │ └─┬ mime-types@2.0.7
  │   └── mime-db@1.5.0
  ├── utils-merge@1.0.0
  └── vary@1.0.0

1 个答案:

答案 0 :(得分:2)

正如聊天中所讨论的,您将需要安装express local而不是global。

本地安装并保存到package.json:

npm install express --save

快速文档:

http://expressjs.com/

另请注意:以下命令将帮助您确定全局安装的软件包。全局安装是可以的,但有时需要一些额外的基础工作。 (IE,为系统路径添加适当的路径)

查看全球套餐:

npm list -g --depth=0

全球与本地:

关于全球与本地的一些额外阅读。

http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation