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
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

