others-How to solve `Cannot find module` error when using npm/express and morgan?

1. Purpose

In this post , I would demonstrate how to solve the following error when using express and morgan with node.js:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'morgan'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/opt/node_services/hello_world/app.js:3:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

The core error is:

Error: Cannot find module 'morgan'

But I did add the module morgan in my node.js project as follows:

npm install morgan -g

Why did not that work for me?


2. The solution

2.1 The code

Here is the code that caused the problem:

const express=require("express");
const process=require("process");
const morgan = require('morgan')

const app=express();
app.use(morgan('combined'));

var appname = "default";
var port = 8080;

app.listen(port,()=>{
    console.log('listening to port '+port+' on '+appname);
});

app.get('/hello/:name',(req,res)=>{
    var name = req.params.name;
    res.send("hello "+name+" from "+appname);
});

And I have installed the morgan as follows:

npm install morgan -g

But when I run the following command:

node app.js

I got this error:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'morgan'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/opt/node_services/hello_world/app.js:3:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)



2.2 The solution

TL;DR, here is the solution :

 ✘ ⚡ npm install --save morgan

+ [email protected]
added 5 packages from 3 contributors in 188.488s

2 packages are looking for funding
  run `npm fund` for details

Now run the app again:

 ✘ ⚡ node app.js server4 8084
appport= 8084
listening to port 8084 on server4

It’s working!



2.3 Why did this solution work?

The key difference is the --save option in the npm install command, what does it do?

npm install without specifying a package name will install the dependencies in your package. json . npm install --save will install the package and update your package.

For details, here is the official option list about save :

npm install saves any specified packages into dependencies by default. Additionally, you can control where and how they get saved with some additional flags:

  • -P, --save-prod: Package will appear in your dependencies. This is the default unless -D or -O are present.

  • -D, --save-dev: Package will appear in your devDependencies.

  • -O, --save-optional: Package will appear in your optionalDependencies.

  • --no-save: Prevents saving to dependencies.

    When using any of the above options to save dependencies to your package.json, there are two additional, optional flags:

  • -E, --save-exact: Saved dependencies will be configured with an exact version rather than using npm’s default semver range operator.

  • -B, --save-bundle: Saved dependencies will also be added to your bundleDependencies list.


3. Summary

In this post, I demonstrated how to solve the Error: Cannot find module 'morgan' error when using express/morgan with node.js, the key point is to call --save when using npm install. That’s it, thanks for your reading.

-->