Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Mixing CommonJS and ES Modules | Modern Modules and Core Node Utilities
Working with Modules and Packages in Node.js

bookMixing CommonJS and ES Modules

When working on Node.js projects, you may encounter both CommonJS and ES Modules in the same codebase. Node.js originally used CommonJS (require/module.exports), but now also supports ES Modules (import/export). Mixing these two systems can be challenging due to differences in syntax, file extensions, and loading behavior. Compatibility issues arise because not all features are available when importing one type of module into another. You should understand the strategies for using both systems together, as well as the limitations you might face.

One common strategy is to decide on a primary module system for your project and use compatibility features only when necessary. For example, you might keep most of your code in CommonJS for compatibility with older tools, but use ES Modules for newer code or when you need features like import.meta.url. Node.js allows you to specify the module type in your package.json file using the "type" field: "commonjs" or "module". Files with .cjs are always treated as CommonJS, while .mjs files are always ES Modules, regardless of the "type" field. This gives you flexibility to mix module types within a project, but you should be aware of the limitations and differences in how modules are loaded and executed.

math.cjs

math.cjs

app.mjs

app.mjs

copy

The code example shows how you can use a CommonJS module inside an ES Module in Node.js. The math.cjs file uses CommonJS syntax, exporting an add function with module.exports.add = (a, b) => a + b;. This function adds two numbers and returns the result.

The app.mjs file is an ES Module. ES Modules cannot use require directly, so you import the createRequire function from the built-in module package. By calling createRequire(import.meta.url), you create a require function that works in the context of the current ES Module file. You then use this require to import the CommonJS module (math.cjs).

When you call math.add(2, 3), the code outputs 5 to the console. This approach allows you to mix CommonJS and ES Modules in the same project, using the strengths of each system where needed.

question mark

When should you choose CommonJS over ES Modules in a Node.js project?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What are the main limitations when mixing CommonJS and ES Modules?

Can you explain how to import an ES Module into a CommonJS file?

Are there any best practices for choosing between CommonJS and ES Modules in a project?

Awesome!

Completion rate improved to 7.14

bookMixing CommonJS and ES Modules

Swipe to show menu

When working on Node.js projects, you may encounter both CommonJS and ES Modules in the same codebase. Node.js originally used CommonJS (require/module.exports), but now also supports ES Modules (import/export). Mixing these two systems can be challenging due to differences in syntax, file extensions, and loading behavior. Compatibility issues arise because not all features are available when importing one type of module into another. You should understand the strategies for using both systems together, as well as the limitations you might face.

One common strategy is to decide on a primary module system for your project and use compatibility features only when necessary. For example, you might keep most of your code in CommonJS for compatibility with older tools, but use ES Modules for newer code or when you need features like import.meta.url. Node.js allows you to specify the module type in your package.json file using the "type" field: "commonjs" or "module". Files with .cjs are always treated as CommonJS, while .mjs files are always ES Modules, regardless of the "type" field. This gives you flexibility to mix module types within a project, but you should be aware of the limitations and differences in how modules are loaded and executed.

math.cjs

math.cjs

app.mjs

app.mjs

copy

The code example shows how you can use a CommonJS module inside an ES Module in Node.js. The math.cjs file uses CommonJS syntax, exporting an add function with module.exports.add = (a, b) => a + b;. This function adds two numbers and returns the result.

The app.mjs file is an ES Module. ES Modules cannot use require directly, so you import the createRequire function from the built-in module package. By calling createRequire(import.meta.url), you create a require function that works in the context of the current ES Module file. You then use this require to import the CommonJS module (math.cjs).

When you call math.add(2, 3), the code outputs 5 to the console. This approach allows you to mix CommonJS and ES Modules in the same project, using the strengths of each system where needed.

question mark

When should you choose CommonJS over ES Modules in a Node.js project?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
some-alt