ES Modules Logo ESModules.com

Debugging & Troubleshooting

Solve common ES Modules errors and issues.

CORS Errors

Error Message:

Cross-origin request blocked: file:///path/to/module.js

✅ Solutions:

  • Use a local development server (not file://)
  • Configure CORS headers on the server
  • Use a dev server like Vite, Live Server, or http-server

Missing File Extension

Error Message:

Failed to resolve module specifier "utils"

❌ Wrong:

import utils from './utils';

✅ Correct:

import utils from './utils.js';

Cannot use import statement outside a module

Error Message:

SyntaxError: Cannot use import statement outside a module

✅ Solutions:

  • Browser: Add type="module" to script tag
  • Node.js: Add "type": "module" to package.json
  • Node.js: Use .mjs file extension

ERR_REQUIRE_ESM

Error Message:

require() of ES Module not supported

You're trying to require() an ES Module. Use import instead:

❌ Wrong:

const module = require('esm-module');

✅ Correct:

import module from 'esm-module';

Debugging Tools

Browser DevTools

  • Check Network tab for failed requests
  • Use Sources tab to set breakpoints
  • Console shows import errors clearly
  • Application tab shows module graph

Node.js Debugging

# Run with inspector
node --inspect app.js

# Verbose loader output
node --loader-trace app.js

Common Pitfalls Checklist

File extensions: Always include .js extension in imports
Script type: Use type="module" in HTML
CORS: Use a development server, not file:// protocol
Package.json: Add "type": "module" for Node.js
Paths: Use relative (./) or absolute (/) paths

Continue Learning