Common Node.js and npm Errors and How to Fix Them
Test setup: Ubuntu 24.04 with Node 18, 20 and 22, Webpack 5.113 and Chrome 153, November 29, 2026. Each fix linked below says whether we reproduced that error.
Node.js errors usually come from one of five places: loading a module, installing packages, starting a server on a port that’s taken, a network call that nothing answers or the browser refusing something your code asked for. The first line of the error and the file it names tell you which.
| Message | Where it happens | Fix |
|---|---|---|
SyntaxError: Cannot use import statement outside a module |
Loading modules | Cannot use import statement outside a module |
Error: Cannot find module / MODULE_NOT_FOUND |
Loading modules | Cannot find module |
Module not found: Error: Can't resolve / Field 'browser' doesn't contain a valid alias configuration |
Bundling with Webpack | Field ‘browser’ alias configuration |
npm error code ERESOLVE |
Installing packages | npm ERESOLVE |
Error: listen EADDRINUSE: address already in use |
Starting a server | Address already in use |
Error: connect ECONNREFUSED 127.0.0.1:3000 |
Network calls | ECONNREFUSED |
blocked by CORS policy |
The browser | CORS error |
Diagnose before you fix
node -v
npm -v
npm ls <package> # is it installed, and which version
Then read the whole error, not just the first line. Node prints the file and line that failed; Webpack prints every path it tried. The fix usually sits in those lines.
Habits that prevent most Node errors
- Pin your Node version per project in
.nvmrcor theenginesfield so every machine runs the same one. - Pick one module system per package: set
"type": "module"for ES modules, or leave it out and userequire. - Start relative imports with
./or../. A bare name likecomponents/Buttonmeans “look in node_modules”. - Commit
package-lock.jsonand usenpm ciin CI.
Every Node and npm error we cover
Module errors
- Cannot Use Import Statement Outside a Module: The FixNode or the browser is reading your file as a classic script. Add "type": "module", rename to .mjs or use require, with fixes for Jest and script tags too.
- Error: Cannot Find Module in Node.js: How to Fix ItNode couldn't resolve the module path. Check the package is installed, the relative path and extension are right and that you're running from the right folder.
- Field 'Browser' Doesn't Contain a Valid Alias ConfigurationWebpack couldn't resolve one of your imports. Check the import starts with ./, the file name's case and the extensions Webpack looks for. Tested fixes inside.
Install errors
- npm ERR ERESOLVE Unable to Resolve Dependency Tree: FixTwo packages want different versions of one peer dependency. Read the Found and Could not resolve lines, install a matching version or use --legacy-peer-deps.
Network errors
- Error: connect ECONNREFUSED: Causes and Fixes in Node.jsNothing accepted the connection at that address and port. Check the service is running, the port is right and localhost resolves to the address you expect.
- EADDRINUSE: Address Already in Use: How to Fix It in NodeAnother process is already listening on that port. Find it with lsof or netstat, stop it or pick another port. Fixes for Node, Python, Mac, Linux and Windows.
Browser errors
- CORS Error: What It Means and How to Fix It ProperlyThe browser blocked a request to another origin because the server didn't allow it. Fix it on the server with the right headers, or proxy it during development.
- Error with Permissions-Policy Header: Unrecognized FeatureThe site sends a Permissions-Policy header naming a feature your browser doesn't support, like attribution-reporting or interest-cohort. It's harmless.
FAQ
How do I check my Node version?
Run node -v. Many module errors depend on it: Node 22 loads a .js file that uses import even without "type": "module", while Node 18 throws an error.
Is it safe to delete node_modules?
Yes. It's rebuilt from package.json and the lockfile by npm install. Keep package-lock.json unless you mean to change dependency versions.
How do I clear the npm cache?
npm cache verify checks it and removes bad entries. npm cache clean --force deletes it. Clearing the cache rarely fixes install errors on its own.
