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 .nvmrc or the engines field so every machine runs the same one.
  • Pick one module system per package: set "type": "module" for ES modules, or leave it out and use require.
  • Start relative imports with ./ or ../. A bare name like components/Button means “look in node_modules”.
  • Commit package-lock.json and use npm ci in CI.

Every Node and npm error we cover

Module errors

Install errors

Network errors

Browser errors

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.