Nodejs Express Start Error: Error:cannot find module ' xxx ', this is because of missing module reference.
For example, I used ' express-session ' in my code, but I didn't add ' express-session ' dependencies to the "dependencies" item in the Package.json file.
D:\nodejs\myapp>set Debug=myapp & npm start
> myapp@0.0.0 start D:\nodejs\myapp
> Node/bin/www< c2/>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 file
{
' 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"
}
}
If you manually add dependencies to the Package.json file, you will need to execute it in your project directory (my directory is: D:\nodejs\myapp): NPM Install
There is another way to do this: NPM Install module name--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)
Remember to replace the ' xxx ' or ' express-session ' I mentioned here with the name of the module in the error message you encountered.
====================== Document Information ===========================
Copyright Disclaimer: Non-commercial free reprint-keep the signature-indicate the source
Signature (by): testcs_dn (micro WX Laugh)
Article source: [Ignorant life, record bit] (HTTP://BLOG.CSDN.NET/TESTCS_DN)