← Back to Node.js, page 10

nodejs-0185 · Easy fresher

Which Node.js system error codes should you know (ENOENT, EADDRINUSE, ECONNREFUSED and others), and what does each one mean?

Node errors from the operating system carry a code (such as ENOENT), a syscall name and often a path or address. The common ones: ENOENT (missing path), EEXIST (already exists), EISDIR and ENOTDIR (wrong kind of path), EACCES and EPERM (permission), ENOTEMPTY (directory not empty), EMFILE (too many open files), EADDRINUSE (port taken), ECONNREFUSED (nothing listening), ECONNRESET (connection closed abruptly), ETIMEDOUT (no answer), ENOTFOUND (DNS failed), EPIPE (write to a closed pipe).

Full answer

Check err.code (a string), not the message text. Other useful fields: err.syscall (open, read, listen, connect), err.path, err.address and err.port.

File system

  • ENOENT: the path does not exist. Often a wrong relative path (relative to the process working directory, not to the file).
  • EEXIST: the path already exists (flag wx, mkdir without recursive).
  • EISDIR, ENOTDIR: a directory used as a file, or a file used as a directory.
  • ENOTEMPTY: rmdir on a directory that has content (use fs.rm with recursive).
  • EACCES, EPERM: permission denied by file mode, owner or policy.
  • EMFILE, ENFILE: too many open files (process or system).
  • ENOSPC: the disk is full.

Network

  • EADDRINUSE: another process (or an old instance of yours) already listens on the port.
  • ECONNREFUSED: the host is reachable, but nothing listens on that port (service down or wrong port).
  • ECONNRESET: the connection was closed abruptly by the peer.
  • ETIMEDOUT: no answer within the time limit (firewall, overloaded host).
  • ENOTFOUND (and EAI_AGAIN): the DNS lookup failed or timed out.
  • EPIPE: writing to a connection or pipe that the other side closed.
  • EADDRNOTAVAIL: the address is not valid on this machine.

Handle them by code: ENOENT may be an expected case, ECONNREFUSED and ETIMEDOUT may deserve a retry, EADDRINUSE and EACCES at start-up should stop the process with a clear message.

javascript
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const net = require('node:net');

(async () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'errno-'));
  fs.writeFileSync(path.join(dir, 'file.txt'), 'x');
  const cases = {
    ENOENT: () => fs.promises.readFile(path.join(dir, 'missing.txt')),
    EISDIR: () => fs.promises.readFile(dir),
    EEXIST: () => fs.promises.writeFile(path.join(dir, 'file.txt'), 'y', { flag: 'wx' }),
    ENOTEMPTY: () => fs.promises.rmdir(dir),
    ENOTDIR: () => fs.promises.readFile(path.join(dir, 'file.txt', 'child')),
    EADDRINUSE: () => new Promise((resolve, reject) => {
      const first = net.createServer().listen(0, '127.0.0.1', () => {
        const second = net.createServer().on('error', (e) => { first.close(); reject(e); });
        second.listen(first.address().port, '127.0.0.1');
      });
    }),
    ECONNREFUSED: () => new Promise((resolve, reject) => {
      const probe = net.createServer().listen(0, '127.0.0.1', () => {
        const { port } = probe.address();
        probe.close(() => net.connect(port, '127.0.0.1').on('error', reject));   // nothing listens on that port now
      });
    }),
  };
  const meaning = {
    ENOENT: 'path does not exist', EISDIR: 'a directory was used as a file', EEXIST: 'path already exists',
    ENOTEMPTY: 'directory is not empty', ENOTDIR: 'a path part is not a directory', EADDRINUSE: 'port is already in use',
    ECONNREFUSED: 'nothing is listening there',
  };
  for (const [name, run] of Object.entries(cases)) {
    const err = await run().then(() => ({ code: 'no error' }), (e) => e);
    console.log(name.padEnd(13), err.code === name ? 'matched' : 'got ' + err.code, '|', String(err.syscall).padEnd(8), '|', meaning[name]);
  }
  fs.rmSync(dir, { recursive: true });
})();

Seven of these errors are produced on purpose and the matching code is checked. ENOTFOUND, ETIMEDOUT and EACCES depend on the network or the user, so they were not run. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): ENOENT matched | open | path does not exist EISDIR matched | read | a directory was used as a file EEXIST matched | open | path already exists ENOTEMPTY matched | rmdir | directory is not empty ENOTDIR matched | open | a path part is not a directory EADDRINUSE matched | listen | port is already in use ECONNREFUSED matched | connect | nothing is listening there

Key points

  • Compare err.code, not the message text.
  • Use err.syscall, err.path and err.address for context.
  • Know the file codes: ENOENT, EEXIST, EISDIR, ENOTEMPTY, EACCES, EMFILE.
  • Know the network codes: EADDRINUSE, ECONNREFUSED, ECONNRESET, ETIMEDOUT, ENOTFOUND.
  • Decide per code: expect, retry or fail loudly.

Common mistakes

  • Parsing the error message text instead of using err.code.
  • Treating every ENOENT as a bug even when the file is optional.
  • Retrying EADDRINUSE or EACCES forever at start-up.
  • Using a relative path and getting ENOENT from a different working directory.
  • Swallowing the error and losing the path and syscall.

Follow-ups

  • What is the difference between EACCES and EPERM?
  • Why can ENOTFOUND be temporary?
  • How do you check whether a file exists without a race?

Tips

  • Group the codes into file and network.
  • Say 'use err.code'.
  • Mention what you would do for each.