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.
Unless you haven’t installed the
express module, you can run the following
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 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
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
Error: Cannot find module 'express' error message, you can if the
express library is within your
node_modules which is available via the
A simple search within the
package.json can help, and you should the following within the
If the above is not present, you need to either do the
global install as stated in the previous section.
Solution-3: Set NODE_PATH for your node_modules
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
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.
For Windows environments, the following command works
When working within
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.