Noejs express start error: Error: Cannot find module'xxx'

Keywords: Session npm JSON Mongoose

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.

  1. D:\nodejs\myapp>set DEBUG=myapp & npm start  
  2.   
  3. > myapp@0.0.0 start D:\nodejs\myapp  
  4. > node ./bin/www  
  5.   
  6. module.js:340  
  7.     throw err;  
  8.     ^  
  9.   
  10. Error: Cannot find module 'express-session'  
  11.     at Function.Module._resolveFilename (module.js:338:15)  
  12.     at Function.Module._load (module.js:289:25)  
  13.     at Module.require (module.js:366:17)  
  14.     at require (module.js:385:17)  
  15.     at Object.<anonymous> (D:\nodejs\myapp\app.js:6:15)  
  16.     at Module._compile (module.js:435:26)  
  17.     at Object.Module._extensions..js (module.js:442:10)  
  18.     at Module.load (module.js:356:32)  
  19.     at Function.Module._load (module.js:313:12)  
  20.     at Module.require (module.js:366:17)  
package.json file

  1. {  
  2.   "name": "myapp",  
  3.   "version": "0.0.0",  
  4.   "private": true,  
  5.   "scripts": {  
  6.     "start": "node ./bin/www"  
  7.   },  
  8.   "dependencies": {  
  9.     "body-parser": "~1.15.2",  
  10.     "cookie-parser": "~1.4.3",  
  11.     "debug": "~2.2.0",  
  12.     "ejs": "^2.5.5",  
  13.     "express": "~4.14.0",  
  14.     "express-session": "^1.14.2",  
  15.     "mongoose": "^4.7.6",  
  16.     "morgan": "~1.7.0",  
  17.     "serve-favicon": "~2.3.0"  
  18.   }  
  19. }  
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

Another method is to execute: npm install module name -- save

  1. D:\nodejs\myapp>npm install express-session --save  
  2. express-session@1.14.2 node_modules\express-session  
  3. ├── on-headers@1.0.1  
  4. ├── cookie-signature@1.0.6  
  5. ├── utils-merge@1.0.0  
  6. ├── cookie@0.3.1  
  7. ├── parseurl@1.3.1  
  8. ├── depd@1.1.0  
  9. ├── crc@3.4.1  
  10. └── uid-safe@2.1.3 (base64-url@1.3.3, random-bytes@1.0.0)  
Remember to replace the'xxx'or'express-session' I mentioned here with the module name suggested in the error message you encountered.

Posted by aquila125 on Thu, 07 Feb 2019 00:48:17 -0800