nodejs express 启动报错:Error: Cannot find module 'xxx'

nodejs express 启动报错:Error: Cannot find module 'xxx',这是因为缺少模块的引用。

比如我在代码中使用了'express-session',但是却没有在 package.json 文件的 "dependencies" 项中添加 'express-session' 的依赖

D:\nodejs\myapp>set DEBUG=myapp & npm start

> [email protected] 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. (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
[email protected] node_modules\express-session
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected] ([email protected], [email protected])
记得把我这里提到的'xxx'或 'express-session' 替换为你遇到 的错误信息中提示的模块名称哦!

======================文档信息===========================

版权声明:非商用自由转载-保持署名-注明出处

署名(BY) :testcs_dn(微wx笑)

文章出处:[无知人生,记录点滴](http://blog.csdn.net/testcs_dn)

你可能感兴趣的:(nodejs express 启动报错:Error: Cannot find module 'xxx')