PromptHub
Node.js Error ERR_MODULE_NOT_FOUND Intermediate 14 views

ERR_MODULE_NOT_FOUND

Node.js cannot find the specified ES module file.

Explanation

ERR_MODULE_NOT_FOUND occurs when Node.js cannot resolve an ES module import. This is similar to MODULE_NOT_FOUND but specifically for ES modules (using import/export syntax). Common causes include missing file extensions in import paths (required for ES modules), incorrect file paths, missing node_modules, or package.json missing the "type": "module" field. Unlike CommonJS require(), ES module imports require explicit file extensions.

Common Causes

  • Missing file extension in import path
  • package.json missing "type": "module"
  • node_modules not installed
  • File path typo
  • Case sensitivity mismatch on Linux

Solution

Add file extensions to import paths (e.g., import from './utils.js' not './utils'). Ensure package.json has "type": "module" for ES module usage. Run npm install to restore node_modules. Check file paths are correct (case-sensitive on Linux). Use the import map feature or path aliases for cleaner imports. Verify the file actually exists at the specified path.

Code Example

// ERR_MODULE_NOT_FOUND: missing file extension
import { helper } from './utils';  // Error!

// Fix: add .js extension
import { helper } from './utils.js';

// For package.json to enable ES modules
{
    "name": "my-app",
    "type": "module",  // enables import/export
    "main": "index.js"
}

// CommonJS require doesn't need extensions
const { helper } = require('./utils'); // works

// For nested imports
import config from './config/index.js';  // explicit path

// package.json not found for dependency
// Fix: run npm install in the correct directory
npm install

// For TypeScript ESM
// tsconfig.json
{
    "compilerOptions": {
        "module": "ESNext",
        "moduleResolution": "bundler"
    }
}

Error Information

Language

javascript

Framework

nodejs

Difficulty

Intermediate

Views

14

Related Errors