Cyberithub

Solved "error: cannot find module express" in Node.js

Advertisements

In this article, we will see how to solve "error: cannot find module express" if you are also getting this error. Last night when I was trying run my node.js program, I noticed an unexpected "error: cannot find module express" on the output and then I thought although it looks like a simple error but there might be lot of folks working on Node.js end up getting this error due to various reasons. So before fixing this error I thought to write an article about this explaining all the possible reasons for this error and the solution that you can try to fix this error. So without further delay let's begin !!

 

Solved "error: cannot find module express" in Node.js

Solved "error: cannot find module express" in Node.js

Also Read: Solved: curl token "The input is not a valid base 64 encoded string" error

As stated earlier, I was trying to run my below app.js program and then suddenly I noticed "error: cannot find module express" on the output as you can see below.

cyberithub@ubuntu:~$ node app.js
internal/modules/cjs/loader.js:638
throw err;
^

Error: Cannot find module 'express'
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> (/home/cyberithub/app.js:1:15)
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)

While "error: cannot find module express" could come due to many reasons but most of the time it is due to missing node.js express module in your System. So to fix this error you just need to install this module in your System. There are various ways to install express module in your System. We will see all the possible methods below.

 

Method 1: Using npm

If you are working on Node.js application then must have node and npm installed in your System irrespective of the System type you are using. So the best way to install express module is through npm utility only. You just need to use either npm install express or npm install express --save command to install the module as shown below.

cyberithub@ubuntu:~$ npm install express
npm WARN saveError ENOENT: no such file or directory, open '/home/cyberithub/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/cyberithub/package.json'
npm WARN cyberithub No description
npm WARN cyberithub No repository field.
npm WARN cyberithub No README data
npm WARN cyberithub No license field.

+ express@4.18.2
added 57 packages from 42 contributors and audited 57 packages in 2.79s

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

found 0 vulnerabilities

You can also give it global access by using -g flag with npm install command as shown below. Also, to give access globally you need to install using sudo or root access. Or else, you will end up with Error: EACCES: permission denied, access '/usr/local/lib'.

cyberithub@ubuntu:~$ sudo npm install -g express --save
+ express@4.18.2
added 57 packages from 42 contributors in 2.579s

 

Method 2: Using apt or apt-get 

If by any chance you are using a Ubuntu/Debian/Linux Mint based systems, then you can also use apt or apt-get utility to install express module in your System. You just need to run sudo apt install node-express or sudo apt-get install node-express command as shown below.

Advertisements
cyberithub@ubuntu:~$ sudo apt install node-express
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libfwupdplugin1 libllvm11 libxmlb1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
node-accepts node-array-flatten node-body-parser node-bytes node-content-disposition node-content-type node-cookie node-cookie-signature node-depd
node-encodeurl node-escape-html node-etag node-finalhandler node-fresh node-http-errors node-iconv node-ipaddr.js node-media-typer node-merge-descriptors
node-methods node-negotiator node-on-finished node-parseurl node-path-to-regexp node-proxy-addr node-range-parser node-raw-body node-send
node-serve-static node-setprototypeof node-statuses node-toidentifier node-type-is node-utils-merge node-vary
Suggested packages:
node-express-generator
The following NEW packages will be installed:
node-accepts node-array-flatten node-body-parser node-bytes node-content-disposition node-content-type node-cookie node-cookie-signature node-depd
node-encodeurl node-escape-html node-etag node-express node-finalhandler node-fresh node-http-errors node-iconv node-ipaddr.js node-media-typer
node-merge-descriptors node-methods node-negotiator node-on-finished node-parseurl node-path-to-regexp node-proxy-addr node-range-parser node-raw-body
node-send node-serve-static node-setprototypeof node-statuses node-toidentifier node-type-is node-utils-merge node-vary
0 upgraded, 36 newly installed, 0 to remove and 24 not upgraded.
Need to get 425 kB of archives.
After this operation, 2,452 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...............................................................

After installing the module, I again tried to run my program and see if it fixed the error. This time I noticed that it indeed solved the error as you can see below.

cyberithub@ubuntu:~$ node app.js
Sample app listening at http://:::8081

As said earlier, most the time you will get "error: cannot find module express" due to missing express module in your System but it is not always the case. Sometimes you might have express module already installed in your System but still you are getting this error. This mostly happens because express is not available in the location where system is expecting it to be. So to solve this problem, you either need to update the installed path to the System by using environment variables or you need to place the module in a location where system is expecting it to be. You can choose either of the solution depending on which suits you the best.

Hope above solution should be enough to solve your "error: cannot find module express" problem. Please let me know your feedback in the comment box !!

Leave a Comment