nodejs express 启动报错:Error: Cannot find module 'xxx',这是因为缺少模块的引用。比如我在代码中使用了'express-session',但是却没有在 package.json 文件的 "dependencies" 项中添加 'express-session' 的依赖
- D:\nodejs\myapp>set DEBUG=myapp & npm start
-
- > myapp@0.0.0 start D:\nodejs\myapp
- > node ./bin/www
-
- module.js:340
- throw err;
- ^
-
- Error: Cannot find module 'express-session'
- at Function.Module._resolveFilename (module.js:338:15)
- at Function.Module._load (module.js:289:25)
- at Module.require (module.js:366:17)
- at require (module.js:385:17)
- at Object.<anonymous> (D:\nodejs\myapp\app.js:6:15)
- at Module._compile (module.js:435:26)
- at Object.Module._extensions..js (module.js:442:10)
- at Module.load (module.js:356:32)
- at Function.Module._load (module.js:313:12)
- at Module.require (module.js:366:17)
package.json 文件
- {
- "name": "myapp",
- "version": "0.0.0",
- "private": true,
- "scripts": {
- "start": "node ./bin/www"
- },
- "dependencies": {
- "body-parser": "~1.15.2",
- "cookie-parser": "~1.4.3",
- "debug": "~2.2.0",
- "ejs": "^2.5.5",
- "express": "~4.14.0",
- "express-session": "^1.14.2",
- "mongoose": "^4.7.6",
- "morgan": "~1.7.0",
- "serve-favicon": "~2.3.0"
- }
- }
如果你手动在 package.json 文件中添加依赖的话,就需要在你的项目目录(我的目录是:D:\nodejs\myapp)下执行:npm install
还有一个方法是执行:npm install 模块名称 --save
- D:\nodejs\myapp>npm install express-session --save
- express-session@1.14.2 node_modules\express-session
- ├── on-headers@1.0.1
- ├── cookie-signature@1.0.6
- ├── utils-merge@1.0.0
- ├── cookie@0.3.1
- ├── parseurl@1.3.1
- ├── depd@1.1.0
- ├── crc@3.4.1
- └── uid-safe@2.1.3 (base64-url@1.3.3, random-bytes@1.0.0)
记得把我这里提到的'xxx'或 'express-session' 替换为你遇到 的错误信息中提示的模块名称哦!
来自 http://blog.csdn.net/testcs_dn/article/details/54236189