Noejs express start error: Error: Cannot find module'xxx', which is due to the lack of module references.
For example, I used'express-session'in my code, but did not add a'express-session' dependency to the dependencies item in the package.json file.
package.json file
- 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)
If you manually add dependencies to the package.json file, you need to execute under your project directory (my directory is: D: nodejs myapp): npm install
- {
- "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"
- }
- }
Another method is to execute: npm install module name -- save
Remember to replace the'xxx'or'express-session' I mentioned here with the module name suggested in the error message you encountered.
- 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)