The internet has come a long way from clunky HTML sites that barely have styling. Now we have advanced frameworks for web development that help us get modern and well-designed sites up and running in a matter of minutes.
However, with these new frameworks also come new challenges and complications. This article discusses the “Error: Cannot find module express” problem and how you can get rid of the issue.
Also read: How to fix ‘React-scripts: Command not found’ error?
Install the required package
As you can probably guess from the error message, the module express is missing from your package dependencies. This can be resolved by opening a terminal, navigating your project’s root directory and typing the following command.
npm install express
Once the installation is complete, restart your project, and you should be good to go. If the command mentioned above doesn’t work, you can try adding the –save flag to see if that works.
npm install --save express
Reinitialise your project
If installing express didn’t do the trick, you might have to reinitialise your project to freshen up all the dependencies you’re using. This can be done by deleting the node_modules folder and the package-lock.json file. Just open a terminal, head over to your project’s root folder and run the following commands one at a time.
rm -rf node_modules package-lock.json
npm install
If you get an error saying you don’t have appropriate permissions to carry out these tasks, prefix sudo to both the commands.
Manually add the package
If installing using the package manager isn’t working for you, you can manually install the express package. First, open your package.json file and add the following lines to the dependencies section.
{
// ....
"dependencies": {
"express": "^4.18.0",
// ....
},
}
Be sure to check and add the latest version of express in the line (4.18.0 at the time of writing). Once the line is added, open a terminal in the root folder of your project and run this command.
npm install
Also read: How to fix ‘Module not found: Error: Can’t resolve’?