This error occurs when Node.js cannot locate a module specified in a require() or import statement. Common causes include typos in the file path, missing files, incorrect relative paths, or uninstalled dependencies. The error typically happens with local file imports that point to non-existent paths or third-party packages that haven't been installed.
When you use require() or import in Node.js, it searches for the specified module following a specific resolution order: built-in core modules (like 'fs', 'path'), npm packages in node_modules, and finally relative or absolute file paths. This error indicates that the module cannot be found in any of these locations. The error message shows the module name you tried to require and the file that contains the require statement, helping you identify the problem. This is typically a file system issue—the file doesn't exist, the path is wrong, or the file was deleted.
First, confirm that the file you're trying to require actually exists at the location specified:
# Check if the file exists from your project root
ls -la lib/helper.js
# Or if it's in a subdirectory
ls -la src/lib/helper.js
# If it's a directory with index.js
ls -la lib/helper/index.jsIf the file doesn't exist, you need to either create it or adjust your require path to point to the correct file. If you recently deleted a file and this error started appearing, consider restoring it from version control using git checkout.
Carefully review the require statement for any spelling mistakes:
// Bad - typo in filename
const helper = require('./lib/halper'); // typo: halper instead of helper
// Good - correct filename
const helper = require('./lib/helper');On Linux and macOS, filenames are case-sensitive, so these are treated as different files:
# These are DIFFERENT files on Linux/macOS
Helper.js # capital H
helper.js # lowercase hOn Windows, filenames are case-insensitive, so both would work—but if you move code to Linux/macOS, it will break. Ensure your require statement matches the actual filename's case exactly.
Verify that your relative path is correct based on where the requiring file is located:
Project structure:
app.js
lib/
helper.js
// In app.js - correct relative path
const helper = require('./lib/helper');If your file structure is different, adjust the path accordingly:
Project structure:
src/
app.js
lib/
helper.js
// In src/app.js
const helper = require('./lib/helper');
// If helper.js is in a different location
src/
app.js
utils/
helper.js
// In src/app.js, use proper relative path
const helper = require('./utils/helper');
// To go up one directory level and back down
src/
config/
app.js
lib/
helper.js
// In src/config/app.js, go up with ../
const helper = require('../lib/helper');Node.js automatically resolves certain file extensions for CommonJS. When you require a file without specifying an extension, Node.js tries these in order:
const helper = require('./lib/helper');
// Node.js tries in this order:
// 1. ./lib/helper (as a directory with index.js)
// 2. ./lib/helper.js
// 3. ./lib/helper.json
// 4. ./lib/helper/index.jsIf your file is named helper.js, you can require it as require('./lib/helper') without the extension. Node.js will find it. But if none of these forms exist, the error occurs.
For ES modules (import/export), you MUST include the file extension:
// CommonJS (no extension needed)
const helper = require('./lib/helper');
// ES modules (extension required)
import helper from './lib/helper.js';If you're trying to require a third-party package (not a local file), ensure it's installed:
# Check if the package is listed in package.json
grep 'package-name' package.json
# Install the missing package
npm install package-name
# If the package is in package.json but not installed
npm install
# Verify it was installed
ls node_modules/package-nameExample for a specific package:
# Install lodash if missing
npm install lodash
# Then require it
const _ = require('lodash');If you've verified all paths and files exist but the error continues, your node_modules directory may be corrupted or outdated:
# Remove the corrupted node_modules and lock file
rm -rf node_modules package-lock.json
# Reinstall all dependencies from package.json
npm install
# If npm cache is corrupted, clear it
npm cache clean --force
# Then try your code again
node app.jsThis ensures all dependencies are fresh, properly installed, and their versions match your package.json.
To debug module resolution issues, Node.js provides the require.resolve() function that shows what path Node.js actually resolves:
try {
const resolvedPath = require.resolve('./lib/helper');
console.log('Module found at:', resolvedPath);
} catch (e) {
console.log('Module resolution failed:', e.message);
}You can also enable detailed module resolution logging:
NODE_DEBUG=module node app.jsThis prints extensive information about where Node.js searches for modules, helping you understand the resolution process.
For complex projects with many interdependencies, consider using a bundler like webpack, esbuild, or Parcel that can pre-bundle all dependencies and provide better error messages. Bundlers can also help catch module resolution issues at build time rather than runtime.
Circular dependencies can also cause module resolution failures. If module A requires B and module B requires A, you may see this error or related initialization errors. Break circular dependencies by:
1. Restructuring your code to eliminate the dependency cycle
2. Moving shared code to a third module that both depend on
3. Using lazy require (requiring inside a function rather than at module scope)
For ES modules, the module resolution is stricter than CommonJS. All relative paths must start with './', '../', or '/', and file extensions are mandatory. This provides better static analysis and tooling support but requires more explicit path specifications.
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