Fix Common Node.js Errors — A Guide for Beginners in India

If you are learning Node.js in a college lab, a bootcamp, or your first job in an Indian startup, you will hit a small, repeating set of errors long before you hit anything exotic. Most beginner Node.js problems are not bugs in your logic at all — they are environment issues: a missing package, a wrong file path, a locked-down folder, or a server that simply isn't running yet. This guide covers the errors beginners run into most often, with the exact fix for each.

1. Error: Cannot find module

This is the error every Node.js beginner sees in their first week. It means require() orimport could not locate the file or package you referenced.

Error: Cannot find module './utils'
Require stack:
- /home/user/app/index.js
  • For local files, always start the path with ./ or ../require('utils') without the dot looks for an npm package, not your file
  • For npm packages, run npm install package-name before requiring it
  • Double-check the filename case — Linux servers (unlike Windows) are case-sensitive
  • If you renamed or moved a file, restart your dev server so the module cache clears

2. EACCES permission denied during npm install

EACCES errors show up when npm tries to write to a system folder your user account does not own — usually while installing a package globally with -g.

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/lib/node_modules

Never fix this by running npm as root or with sudo — that creates permission problems later. The reliable fix is to install a Node version manager such as nvm, which installs Node entirely inside your home directory so npm never needs elevated permissions again.

3. ECONNREFUSED when calling an API or database

ECONNREFUSED means your code tried to open a connection to a host and port, and nothing answered. It is extremely common when a beginner starts their Express server and their frontend at the same time, but the backend hasn't finished booting yet, or when a database like MongoDB or MySQL isn't running locally.

Error: connect ECONNREFUSED 127.0.0.1:27017
  1. Confirm the target service is actually running (e.g. mongod or mysql.server start)
  2. Check the port number in your connection string matches the service's actual port
  3. If using Docker, confirm the container is up and the port is published, not just exposed internally
  4. On shared or cloud environments, check that a firewall rule isn't blocking the port

4. UnhandledPromiseRejection and async errors

Once you start using async/await with databases and APIs, forgetting a try/catch block around an awaited call produces an unhandled rejection warning, and in newer Node versions, the process crashes entirely.

// Broken: no error handling
app.get('/users', async (req, res) => {
  const users = await db.getUsers();
  res.json(users);
});

// Fixed
app.get('/users', async (req, res) => {
  try {
    const users = await db.getUsers();
    res.json(users);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

5. Port already in use (EADDRINUSE)

This happens when you try to start your Express or Node server on a port that another process — often a previous run of the same app that didn't shut down cleanly — is already using.

  • On Windows, find and stop the process with netstat -ano | findstr :3000 then taskkill /PID <pid> /F
  • On Linux or macOS, use lsof -i :3000 then kill -9 <pid>
  • Or simply change your app's port in code or in a .env file

Frequently Asked Questions

Why do I get "Cannot find module" in Node.js?

This error means Node.js could not locate the file or package you tried to require or import. The most common causes are a typo in the path, a missing "./" before a local file path, or forgetting to run npm install for a third-party package.

How do I fix EACCES permission errors with npm?

EACCES errors happen when npm tries to write to a directory your user account cannot access, usually the global node_modules folder. Fix it by changing npm's default global directory to a folder you own, or by using a Node version manager like nvm instead of a system-wide Node install.

What does ECONNREFUSED mean in Node.js?

ECONNREFUSED means your Node.js app tried to connect to a server, database, or API on a specific host and port, but nothing was listening there. Check that the target service is actually running, the port number is correct, and no firewall is blocking the connection.

Paste the error, get the fix

Stuck on a Node.js stack trace? Paste it into AI Error Explainer to get a plain-English explanation and a suggested fix in seconds.

Related articles