I am new to Node.js, try to learn express to build my first web application. I got stuck on my very first sample code and need some help to get it running. Before I post this question, I did search on stack overflow, found some similar questions but still could not fix it.
Error: Cannot find module 'express'
I am using mac os 10.8.2. I have Node.js installed using nvm
.
node.js: 0.8.20 path to node: /Users/feelexit/nvm/v0.8.20/bin/node
path to express: /Users/feelexit/nvm/node_modules/express
here’s my sample code: this file locates at:
/Users/feelexit/WebstormProjects/learnnode/node_modules/index.js
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('welcome to express');
});
app.listen(3000);
when I try to run this command node index.js
I get following error message, please help me to fix it.
Thank you.
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/Users/feelexit/WebstormProjects/learnnode/node_modules/index.js:1:81)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
feelexits-Mac:node_modules feelexit$
Update to answer chovy’s question:
feelexits-Mac:~ feelexit$ npm install
npm ERR! install Couldn't read dependencies
npm ERR! Error: ENOENT, open '/Users/feelexit/package.json'
npm ERR! If you need help, you may report this log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <npm-@googlegroups.com>
npm ERR! System Darwin 12.2.0
npm ERR! command "/Users/feelexit/nvm/v0.8.20/bin/node" "/Users/feelexit/nvm/v0.8.20/bin/npm" "install"
npm ERR! cwd /Users/feelexit
npm ERR! node -v v0.8.20
npm ERR! npm -v 1.2.11
npm ERR! path /Users/feelexit/package.json
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/feelexit/npm-debug.log
npm ERR! not ok code 0
I am new to Node.js, try to learn express to build my first web application. I got stuck on my very first sample code and need some help to get it running. Before I post this question, I did search on stack overflow, found some similar questions but still could not fix it.
Error: Cannot find module 'express'
I am using mac os 10.8.2. I have Node.js installed using nvm
.
node.js: 0.8.20 path to node: /Users/feelexit/nvm/v0.8.20/bin/node
path to express: /Users/feelexit/nvm/node_modules/express
here’s my sample code: this file locates at:
/Users/feelexit/WebstormProjects/learnnode/node_modules/index.js
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('welcome to express');
});
app.listen(3000);
when I try to run this command node index.js
I get following error message, please help me to fix it.
Thank you.
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/Users/feelexit/WebstormProjects/learnnode/node_modules/index.js:1:81)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
feelexits-Mac:node_modules feelexit$
Update to answer chovy’s question:
feelexits-Mac:~ feelexit$ npm install
npm ERR! install Couldn't read dependencies
npm ERR! Error: ENOENT, open '/Users/feelexit/package.json'
npm ERR! If you need help, you may report this log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <npm-@googlegroups.com>
npm ERR! System Darwin 12.2.0
npm ERR! command "/Users/feelexit/nvm/v0.8.20/bin/node" "/Users/feelexit/nvm/v0.8.20/bin/npm" "install"
npm ERR! cwd /Users/feelexit
npm ERR! node -v v0.8.20
npm ERR! npm -v 1.2.11
npm ERR! path /Users/feelexit/package.json
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/feelexit/npm-debug.log
npm ERR! not ok code 0
Why we get “error: cannot find module express”?
Express is a web framework built on Node.js
, and one that allows developers to build minimal and scalable web and mobile applications. It provides features from top performance, API designs, and extensive frameworks built on it.
For us to run Express, we need to the Node Package Manager, popularly known as npm
. However, when we run certain node applications or express codebase, we might experience the error message below
Error: Cannot find module 'express'
This error is quite common and exists because when your code ran, the express
package or module wasn’t found within its environment. In this article, we will discuss the ways you could solve this error.
Solution-1: Using npm
To do anything within Node, the npm
is your gateway to perform a lot of operations, and to solve this error, you will need it.
Normal Install
Unless you haven’t installed the express
module, you can run the following node
command
npm install express
However, if you still run the code and it gives the same issue, you can try a global installation of the express
module as you might be running your Node.js
code from a global environment within your OS.
Global Install
To perform global
install of a node
module or package, you need the -g
flag. This works by place the path for the express
module within the system path
. With these, you can install the express
module globally and be able to access it without raising a not found
error message.
npm install -g express
Output:
added 57 packages, and audited 58 packages in 3s
7 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
ALSO READ: JavaScript Singleton Pattern [In-Depth Tutorial]
Solution-2: Check your package.json file
To solve Error: Cannot find module 'express'
error message, you can if the express
library is within your node_modules
which is available via the package.json
file.
A simple search within the package.json
can help, and you should the following within the package.json
file.
"node_modules/express": {
"version": "4.18.1",
"resolved": "<https://registry.npmjs.org/express/-/express-4.18.1.tgz>",
"integrity": "sha512-zZBcOX9TfehHQhtupq57OF8lFZ3UZi08Y97dwFCkD8p9d/d2Y3M+ykKcwaMDEL+4qyUolgBDX6AblpR3fL212Q==",
...
If the above is not present, you need to either do the normal
or global
install as stated in the previous section.
Solution-3: Set NODE_PATH for your node_modules
If express
module is present within the package.json
file and you have tried the normal and global installation, and still experience the same issues, there is another option.
This option involves setting the NODE_PATH
for your node_modules
. To carry out this operation, you need to install or update express
.
npm install express
Afterwards, you set and link the node path to the place of your node_modules
. For Linux environments, the following command works.
set NODE_PATH=your\\directory\\to\\node_modules;%NODE_PATH%
For Windows environments, the following command works
setx NODE_PATH=%AppData%\\npm\\node_modules
Summary
When working within node.js
and express
, we might face Error: Cannot find module 'express'
error message. There are different reasons for this error message to be thrown at us and understanding the context matters.
If you have not installed express
, you might experience this and if the node_modules
is not set within your OS environment variables, you might experience it too. Therefore, if you try any of the options above, you will be able to deal with the Error: Cannot find module 'express'
error message.
Further Reading
Installing Express (expressjs.com)
setx | Microsoft Learn
Node.js Error: Cannot find module express
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
One common error that you might encounter when running a Node.js application is:
Error: Cannot find module 'express'
This error occurs when you run a Node.js application that requires the Express module, but the module can’t be found in your local node_modules
folder.
Let me show you an example that causes this error and how you can fix it.
How to reproduce this error
Suppose you’re developing a web application using Node.js as your JavaScript runner.
You write the code to import the express
module and create a server at a specific port as shown below:
const express = require("express");
const app = express();
const port = 3000;
app.listen(3000, () => {
console.log(`Example app listening on port ${port}`);
});
app.get("/", (req, res) => {
res.send("Hello World!");
});
Next, you run the code using the node index
or node app.js
command from your terminal.
But you get the following error:
Error: Cannot find module 'express'
This error occurs because Node.js can’t find the express
module inside your project folder.
Node.js needs the package to be installed using npm or Yarn so that it can be used.
How to fix this error
To resolve this error, you need to install the express
module using npm or Yarn.
Here’s the command you need to run:
# If you use npm:
npm install express
# If you use Yarn:
yarn add express
Note that you need to install the package locally in your project folder.
If you previously installed Express using the global -g
option as follows:
Then the error won’t be resolved because Node.js doesn’t search the global package when running your code.
You need to make sure that you’re installing express locally by searching for the express
folder inside the node_modules
folder as shown below:
project
├── app.js
├── node_modules
│ └── express
├── package-lock.json
└── package.json
...
You might also see other related packages that are required by Express as dependencies, but the main point is that Node.js will look for the express
folder when you run the const express = require("express");
line.
If you have the package installed, then the error should be resolved.
Conclusion
The Node.js Error: Cannot find module ’express’ occurs when you try to import the express
module without having it installed on your local project folder.
To resolve this error, run the command npm install express
or yarn add express
from the project root folder.
Installing the package globally won’t resolve this error. You need to install it right on the project.
I hope this tutorial helps. Happy coding! 🙌
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 !!
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.
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 !!