Node.js throws this error when require() or import attempts to load a module from a directory path that does not exist in the filesystem. This commonly happens due to incorrect paths, missing installations, or misconfigured module resolution.
This error occurs when Node.js's module resolution algorithm cannot locate a module at the specified path because the directory or file does not exist. When you use `require()` or `import`, Node.js follows a specific algorithm to search for modules, starting with the exact path provided, then checking node_modules directories, and finally consulting NODE_PATH if configured. The error message shows two key pieces of information: the module path that couldn't be found (e.g., '/nonexistent/lib') and the file that attempted to require it (e.g., '/app.js'). This indicates that the module loader searched for the specified path but found no matching file or directory in the filesystem. This is different from a module existing but having internal errors - this error specifically means the file or directory itself is missing. Node.js cannot proceed with loading because there's nothing to load at the specified location.
First, check if the module file actually exists at the path specified in your error message.
# Navigate to your project root
cd /path/to/your/project
# Check if the file exists
ls -la ./path/to/module.js
# Or check the full directory structure
find . -name "module-name*"If the file doesn't exist, you'll need to either create it, install the missing package, or correct the import path.
If you're importing a local file, ensure you're using the correct relative path prefix. Node.js requires ./ or ../ for relative imports.
Incorrect:
// Missing ./ prefix - Node will look in node_modules
const utils = require('utils/helper');Correct:
// Proper relative path
const utils = require('./utils/helper');For parent directories, use ../:
const config = require('../../config/settings');File paths are case-sensitive on Linux servers but not on macOS/Windows. Verify the casing matches exactly.
Check your code:
// Your code
const util = require('./Utils/helper.js');Check actual filesystem:
ls -la ./utils # lowercase 'utils' directoryFix the import to match the actual filesystem casing:
// Corrected
const util = require('./utils/helper.js');This is a common issue when developing on macOS/Windows but deploying to Linux.
If the module is a package from npm, ensure it's installed and node_modules exists.
# Check if package is in package.json
cat package.json | grep "module-name"
# Install all dependencies
npm install
# Or install a specific package
npm install module-name
# For pnpm
pnpm install
# For yarn
yarn installVerify the module appears in node_modules:
ls -la node_modules/module-nameIf dependencies are corrupted or incomplete, perform a clean reinstall.
# Remove existing installations
rm -rf node_modules package-lock.json
# Reinstall from scratch
npm install
# For pnpm
rm -rf node_modules pnpm-lock.yaml
pnpm install
# For yarn
rm -rf node_modules yarn.lock
yarn installAfter reinstalling, restart your Node.js process or development server.
Node.js resolves relative paths from the current working directory (cwd), not necessarily from the script's location.
Check where your process is running from:
console.log('Working directory:', process.cwd());
console.log('Script location:', __dirname);If they differ and cause issues, use __dirname for reliable paths:
const path = require('path');
const config = require(path.join(__dirname, '../config/settings'));Or change your working directory when starting the app:
cd /correct/directory && node app.jsWhile Node.js auto-resolves .js extensions, some configurations require explicit extensions.
If you're using ES modules (type: "module" in package.json), extensions are often required:
// May fail without extension in ESM
import helper from './utils/helper';
// Explicit extension
import helper from './utils/helper.js';For TypeScript compiled output, ensure .js extensions are used in the compiled code, even if your source uses .ts.
Module Resolution Algorithm:
Node.js follows a specific resolution algorithm for CommonJS modules:
1. If the path starts with './' or '../' or '/', it's treated as a file path
2. Node checks for exact filename, then tries adding .js, .json, .node extensions
3. If it's a directory, Node looks for package.json with "main" field, or index.js
4. For module names without path prefixes, Node searches up the directory tree in node_modules folders
Debugging Module Resolution:
Use NODE_DEBUG environment variable to see detailed resolution logs:
NODE_DEBUG=module node app.jsThis shows every path Node.js attempts when resolving modules, helping identify where the resolution is failing.
ESM vs CommonJS:
ES modules (import/export) have stricter resolution rules than CommonJS (require). ESM requires file extensions and doesn't support directory imports without explicit index.js references in package.json exports field.
Build Tool Considerations:
If using TypeScript, webpack, or other build tools, the module resolution during compilation may differ from runtime Node.js resolution. Check your tsconfig.json paths, webpack resolve.alias, or similar configuration.
Container and Docker Issues:
In Docker containers, ensure your COPY or VOLUME commands include all necessary files. A common mistake is forgetting to copy node_modules or setting .dockerignore to exclude needed files. Always run npm install inside the container if node_modules isn't copied.
Symbolic Links:
If using symlinks (ln -s) for shared modules, verify they point to valid locations. Broken symlinks appear as missing modules. Use ls -la to check symlink targets.
Error: Listener already called (once event already fired)
EventEmitter listener already called with once()
Error: EACCES: permission denied, open '/root/file.txt'
EACCES: permission denied
Error: Invalid encoding specified (stream encoding not supported)
How to fix Invalid encoding error in Node.js readable streams
Error: EINVAL: invalid argument, open
EINVAL: invalid argument, open
TypeError: readableLength must be a positive integer (stream config)
TypeError: readableLength must be a positive integer in Node.js streams