1

我可以正常安装 npm 包,并且我已经通过“npm install express”命令安装了 express。这是正在尝试运行的 app.js 文件:

/* app.js */
var app = require('express').createServer();

app.get('/', function(req, res){
    res.send('hello world');
});

app.listen(3000);

c:\> node app.js

这是错误:

node.js:63
    throw e;
    ^
Error: Cannot find module 'express'
    at loadModule (node.js:275:15)
    at require (node.js:411:14)
    at Object.<anonymous> (/home/node/app/app.js:1:73)
    at Module._compile (node.js:462:23)
    at Module._loadScriptSync (node.js:469:10)
    at Module.loadSync (node.js:338:12)
    at Object.runMain (node.js:522:24)
    at Array.<anonymous> (node.js:756:12)
    at EventEmitter._tickCallback (node.js:55:22)
    at node.js:773:9 

问题,我遵循的错误步骤是什么?

这是安装 express 后的输出:

C:\MYSite>npm install express
npm http GET https://registry.npmjs.org/express
npm http 200 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/connect/2.7.2
npm http GET https://registry.npmjs.org/commander/0.6.1
npm http GET https://registry.npmjs.org/fresh/0.1.0
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/methods/0.0.1
npm http GET https://registry.npmjs.org/cookie/0.0.5
npm http GET https://registry.npmjs.org/buffer-crc32/0.1.1
npm http GET https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/cookie-signature/0.0.1
npm http GET https://registry.npmjs.org/mkdirp/0.3.3
npm http GET https://registry.npmjs.org/send/0.1.0
npm http 304 https://registry.npmjs.org/range-parser/0.0.4
npm http 304 https://registry.npmjs.org/methods/0.0.1
npm WARN package.json methods@0.0.1 No README.md file found!
npm http 304 https://registry.npmjs.org/fresh/0.1.0
npm http 200 https://registry.npmjs.org/commander/0.6.1
npm http GET https://registry.npmjs.org/commander/-/commander-0.6.1.tgz
npm http 304 https://registry.npmjs.org/cookie/0.0.5
npm http 200 https://registry.npmjs.org/connect/2.7.2
npm http 304 https://registry.npmjs.org/buffer-crc32/0.1.1
npm http GET https://registry.npmjs.org/connect/-/connect-2.7.2.tgz
npm http 304 https://registry.npmjs.org/debug
npm http 200 https://registry.npmjs.org/cookie-signature/0.0.1
npm http GET https://registry.npmjs.org/cookie-signature/-/cookie-signature-0.0.1.tgz
npm http 200 https://registry.npmjs.org/mkdirp/0.3.3
npm http GET https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.3.tgz
npm http 304 https://registry.npmjs.org/send/0.1.0
npm http 200 https://registry.npmjs.org/commander/-/commander-0.6.1.tgz
npm http 200 https://registry.npmjs.org/connect/-/connect-2.7.2.tgz
npm http 200 https://registry.npmjs.org/cookie-signature/-/cookie-signature-0.0.1.tgz
npm http 200 https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.3.tgz
npm http GET https://registry.npmjs.org/mime/1.2.6
npm http GET https://registry.npmjs.org/pause/0.0.1
npm http GET https://registry.npmjs.org/formidable/1.0.11
npm http GET https://registry.npmjs.org/bytes/0.1.0
npm http GET https://registry.npmjs.org/qs/0.5.1
npm http 304 https://registry.npmjs.org/mime/1.2.6
npm http 304 https://registry.npmjs.org/bytes/0.1.0
npm http 304 https://registry.npmjs.org/pause/0.0.1
npm http 200 https://registry.npmjs.org/qs/0.5.1
npm http GET https://registry.npmjs.org/qs/-/qs-0.5.1.tgz
npm http 200 https://registry.npmjs.org/formidable/1.0.11
npm http GET https://registry.npmjs.org/formidable/-/formidable-1.0.11.tgz
npm http 200 https://registry.npmjs.org/qs/-/qs-0.5.1.tgz
npm http 200 https://registry.npmjs.org/formidable/-/formidable-1.0.11.tgz
express@3.1.0 node_modules\express
├── methods@0.0.1
├── fresh@0.1.0
├── buffer-crc32@0.1.1
├── cookie-signature@0.0.1
├── cookie@0.0.5
├── range-parser@0.0.4
├── commander@0.6.1
├── debug@0.7.2
├── mkdirp@0.3.3
├── send@0.1.0 (mime@1.2.6)
└── connect@2.7.2 (pause@0.0.1, bytes@0.1.0, formidable@1.0.11, qs@0.5.1)
4

2 回答 2

4

您在本地安装了 express,而不是全局安装

C:\MYSite>npm install express

所以你必须只从这个MYSite文件夹运行。但是您正在从不同的位置运行它。

C:> 节点 app.js

因此它不断给出错误Error: Cannot find module 'express'

于 2013-03-18T11:37:27.440 回答
0

如果您使用 Homebrew 安装节点,即使使用全局安装,您仍然会遇到问题。

您可以通过执行以下操作来修复它:

brew uninstall node

然后使用 nodejs.org 站点上的安装程序安装节点。

然后运行:

npm install -g express

希望这可以帮助某人。

于 2013-07-10T16:56:49.880 回答