How do I enable ES modules in Node.js?
Set 'type': 'module' in your package.json, or use .mjs file extensions. This tells Node.js to treat files as ES modules instead of CommonJS.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in CommonJS vs ES Modules in Node.js: Which Should You Use?
Use CommonJS for existing projects and maximum ecosystem compatibility, since most npm packages were built for it. Use ES modules for new projects wanting the modern standard, static analysis, and consistency with frontend code.
CommonJS is synchronous, runtime-loaded, uses require and module.exports, and allows conditional require. ES modules are async, statically analyzable, use import and export, and imports must be at the top level.
Node.js supports interoperability, but mixing is complex and best avoided. For a clean setup, pick one system per project. If you must mix, use dynamic import() to load ES modules from CommonJS.
Still have questions?
Browse all our FAQs or reach out to our support team
