Node.js errors are easier to fix when you identify which layer failed: JavaScript syntax, object access, module resolution, filesystem permissions, network sockets or asynchronous control flow. A stack trace and error code tell you more than a vague message like “the server is broken.” These ten common errors illustrate how to investigate a real cause without hiding failures or restarting a production process blindly.
1. SyntaxError: invalid JavaScript
SyntaxError means the parser could not interpret the program, often because of missing punctuation, invalid object syntax or a misplaced token. Run a syntax check before deploying a change:
node --check server.js
The reported line and column are clues; the actual mismatch may be earlier. Use formatting and linting to locate missing braces or parentheses. A malformed source file cannot be repaired by retrying the same request at runtime.
2. ReferenceError: name is not defined
A ReferenceError may occur when code tries to use an undeclared identifier. Check spelling, module scope, import paths and the timing of access to variables declared with let or const. Example:
const total = 5;
console.log(total); // declared value
Do not fix a missing variable by silently creating an unrelated global. Trace where the value should have been provided and add a test for the intended scope.
3. TypeError: an operation is not valid for the value
TypeError frequently appears when code calls a function on undefined, reads a property of null or passes a value of the wrong type. Validate data at input boundaries:
function labelUser(user) {
if (!user || typeof user.name !== 'string') {
throw new TypeError('user.name must be a string');
}
return user.name.trim();
}
Optional chaining is useful when a missing value is allowed, but it should not conceal a violated application contract.
4. ERR_MODULE_NOT_FOUND or MODULE_NOT_FOUND
Node cannot resolve an imported or required module. Confirm whether the application uses ECMAScript modules or CommonJS, whether the dependency is installed in the production package, and whether the path and filename case are correct. Local relative imports and package imports follow different resolution rules.
npm ls --depth=0
node --version
If the project works locally but not in CI, inspect the lockfile, install command and runtime Node version. Do not blindly install packages from similarly spelled names.
5. ENOENT: file or directory not found
Filesystem calls may fail with ENOENT when the path does not exist. Relative paths are normally resolved from the process working directory, not necessarily from the source file’s directory. Log the resolved path in a safe development environment and verify that a packaged artifact includes the expected file.
Use node:path with module-relative paths when appropriate. Treat missing configuration and required data as explicit startup errors rather than inventing empty default files in production.
6. EACCES: access denied
EACCES indicates insufficient permission for a filesystem or operating-system action. Check the process user, directory ownership and deployment policies. On Unix-like systems, binding to a privileged port can also require elevated privileges.
Grant the smallest permission the process actually needs. Running the entire application as root to suppress an access error creates a larger security exposure and rarely fixes the underlying configuration.
7. EADDRINUSE: the listening address is busy
A server may fail to start because another process already uses its intended address and port. Verify the configured port, check active listeners with an authorized system tool and determine whether the existing process is expected.
node -e "console.log(process.env.PORT || 3000)"
In production, adjust port configuration through the service manager or load balancer rather than terminating unrelated processes at random.
8. ECONNREFUSED: no service accepted the connection
This error commonly means the target host was reachable but nothing accepted a connection on the chosen port. Confirm hostname, port, listening interface, service health, container network mapping and firewall behavior. A container service bound only to its own loopback interface may not be accessible through the expected network path.
A retry with a short backoff can be appropriate for a temporarily starting dependency, but repeated refusal is evidence to investigate; it is not a reason for an unbounded retry loop.
9. ECONNRESET: an established connection was closed
ECONNRESET indicates a connection reset, often because the peer or an intermediary ended the socket unexpectedly. Review server logs, idle timeouts, connection pools, payload sizes, proxies and network interruptions. A reset does not by itself identify which component caused it.
Retry only operations that are safe to repeat. For state-changing requests, idempotency keys and authoritative readback may be necessary to avoid duplicate effects after an ambiguous network failure.
10. Unhandled promise rejection
An asynchronous operation can reject even when the initiating function returns normally. Always await relevant promises and handle failures at a boundary with enough context to decide whether to recover or fail.
async function loadConfig() {
try {
return await fetchConfig();
} catch (error) {
console.error('Config load failed:', error);
throw error;
}
}
In current Node versions, an unhandled rejection may be raised as an uncaught exception and terminate the process. Do not rely on a global unhandledRejection handler as a substitute for correct local handling and controlled shutdown.
Use a repeatable debugging routine
First reproduce the failure under the same runtime and configuration. Preserve the error code, stack trace and relevant timestamp, while masking credentials and personal data. Next isolate the smallest failing operation, check its inputs and environment, and form one testable hypothesis. Fix the cause, write a regression test and verify that monitoring shows recovery.
For production services, include structured logs, health checks, bounded timeouts and graceful shutdown. A 500 response, process crash and bad output are different classes of failure; avoid one generic recovery action for all three.
Refer to the official Node.js errors reference and process event documentation when checking exact error-code and promise behavior for your installed version.