Matthew C.
—You have a JavaScript dependency in your Node.js project. When you try to import an item from the dependency module, you get the following Node.js error:
Error: Cannot find module '<module name>'
This error occurs when Node.js can’t find a module that you imported into your code. The name of the missing module is provided in the error message. How do you fix this error?
This error is caused by a missing dependency, an incorrect file path, an outdated dependency, or corrupt files. The commands shown in the solution are for the npm package manager. There are equivalent commands for Yarn and pnpm.
When a project’s Node.js dependencies are installed, they are added to the node_modules
folder in the root directory of the project. Check the package.json
file to see if the missing module is in the project’s dependencies
or devDependencies
. If it is not, install the dependency:
npm install <dependency name>
If the dependency is in the package.json
file, make sure that the node_modules
folder is in the root folder of the project and that the missing module is in the node_modules
folder. Also, make sure that the file path in the module import is correct.
The issue may be caused by importing from an outdated module. To fix this, update the dependency to the latest version:
npm update [<pkg>...]
If all else fails, you can try the following:
node_modules
folder:rm -rf node_modules
package-lock.json
file:rm -f package-lock.json
npm cache clean --force
npm install
If you still have an error, you can try to reinstall Node.js and npm to make sure that you have the latest versions and that any corrupted files are replaced.
If you’re looking to get a deeper understanding of how Node application monitoring works, take a look at the following articles:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.