search
node

Fix: ERR_MODULE_NOT_FOUND Node.js error

Quick fix for 'ERR_MODULE_NOT_FOUND' error in Node.js. Learn how to resolve module resolution issues in ES modules and CommonJS.

person By Gautam Sharma
calendar_today January 8, 2026
schedule 2 min read
Node.js ES Modules CommonJS Module Resolution Error Fix

The ‘ERR_MODULE_NOT_FOUND’ error occurs when Node.js cannot locate a module during import, typically due to incorrect file paths, missing file extensions, or module resolution issues in ES modules.


How the Error Happens

❌ Error Scenario:

// ❌ This causes ERR_MODULE_NOT_FOUND
import myModule from './myModule'; // ❌ Missing .js extension in ESM
// Error: Cannot find module './myModule' imported from ...

// ❌ Wrong file extension
import helper from './helper.ts'; // ❌ If file is actually helper.js

✅ Quick Fix - Module Resolution Solutions

Solution 1: Add File Extensions

// ✅ In ES modules, include file extensions
import myModule from './myModule.js'; // ✅ Correct
import { helper } from './utils/helper.js'; // ✅ Include .js extension

// ✅ For JSON files
import config from './config.json' assert { type: 'json' }; // ✅ With assertion

Solution 2: Check Package.json Type

// ✅ For ES modules, add type: "module" to package.json
{
  "name": "my-app",
  "type": "module", // ✅ Enables ES module support
  "main": "index.js"
}
// ✅ For CommonJS, omit type or set to "commonjs"
{
  "name": "my-app",
  "type": "commonjs", // ✅ Explicit CommonJS
  "main": "index.js"
}

Solution 3: Use Absolute Paths

// ✅ Use absolute paths with file:// protocol
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// ✅ Resolve absolute path
import myModule from './myModule.js'; // ✅ Relative to current file
// OR
const modulePath = join(__dirname, 'myModule.js');
import(modulePath); // ✅ Dynamic import with resolved path

Solution 4: Mixed Module Systems

// ✅ Using CommonJS in ES modules
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

// ✅ This allows require() in ES modules
const myModule = require('./myModule.js');
const express = require('express');

Solution 5: Check Node.js Version

# ✅ Verify Node.js version supports ES modules
node --version

# ✅ ES modules supported from Node.js 12+, stable from 14.13.0+
# For older versions, use .mjs extension:
# myModule.mjs instead of myModule.js
Gautam Sharma

About Gautam Sharma

Full-stack developer and tech blogger sharing coding tutorials and best practices

Related Articles

node

Fix: ExperimentalWarning: Importing JSON modules error

Quick fix for 'ExperimentalWarning: Importing JSON modules' error in Node.js. Learn how to properly import JSON files in modern Node.js applications.

January 8, 2026
node

Fix: ERR_PACKAGE_PATH_NOT_EXPORTED error

Quick fix for 'ERR_PACKAGE_PATH_NOT_EXPORTED' error in Node.js. Learn how to resolve module export issues in ES modules and package configurations.

January 8, 2026
node

Fix: ERR_UNKNOWN_FILE_EXTENSION '.ts' error

Quick fix for 'ERR_UNKNOWN_FILE_EXTENSION .ts' error in Node.js. Learn how to properly run TypeScript files in Node.js environments.

January 8, 2026