Integrating a CommonJS (CJS) package into an
ECMAScript Module (ESM) project can be achieved using
the import
statement, with attention to
how the CJS module exports its content. Here's how you
can approach this:
Default Export Handling:
module.exports
, you can
import it as a default import in your ESM code:
javascript// CJS module (example.cjs) module.exports = function exampleFunction() { // function implementation };
In this scenario, the entire module is treated as the default export.javascript// ESM module import exampleFunction from './example.cjs'; exampleFunction();
Named Exports Handling:
exports
or
module.exports
, you can import them
using named imports:
javascript// CJS module (example.cjs) module.exports.namedFunction = function namedFunction() { // function implementation };
Here, each property on thejavascript// ESM module import { namedFunction } from './example.cjs'; namedFunction();
exports
object becomes a named
export.
Namespace Import:
This approach is useful when you want to access multiple exports from the CJS module.javascript// ESM module import * as exampleModule from './example.cjs'; exampleModule.namedFunction();
Considerations:
File Extensions: Ensure that the
CJS module file has the appropriate extension
(e.g., .cjs
) if your project is
configured to distinguish between module types
based on extensions.
Asynchronous Imports: In some
cases, especially when dealing with dynamic
imports or certain module systems, you might need
to use asynchronous import()
to load
CJS modules.
TypeScript Projects: If you're
using TypeScript, enabling the
esModuleInterop
compiler option can
simplify the import process by allowing default
imports from CJS modules.
By following these methods, you can effectively incorporate CJS packages into your ESM project.
Integrating an ECMAScript Module (ESM) into a CommonJS
(CJS) project requires the use of dynamic
import()
statements, as the static
require()
function is not compatible with
ESM's asynchronous nature. Here's how you can achieve
this:
Dynamic Import in CommonJS:
import()
function to load the ESM
module asynchronously:
In this example,javascript// CommonJS module (example.cjs) (async () => { const { default: esmFunction, namedExport } = await import('./example.mjs'); esmFunction(); console.log(namedExport); })();
esmFunction
is the
default export from the ESM module, and
namedExport
is a named export.
Handling Default and Named Exports:
default
property for the default
export and properties corresponding to named
exports:
The CommonJS module can access these exports as shown in the previous example.javascript// ESM module (example.mjs) export const namedExport = 'This is a named export'; export default function esmFunction() { console.log('This is the default export'); }
Considerations:
Asynchronous Nature: Dynamic
imports return a promise, so ensure that your
CommonJS code handles this asynchronous behavior
appropriately, often using async functions and the
await
keyword.
File Extensions: Use the
.mjs
extension for ESM files and
.cjs
for CommonJS files to clearly
distinguish between the two module types.
Node.js Version: Ensure you're using Node.js version 12 or later, as earlier versions do not support ESM.
By following these steps, you can effectively incorporate ESM modules into your CommonJS project.
In Node.js, the createRequire
function
from the module
module allows you to use
CommonJS require
functionality within
ECMAScript Modules (ESM). This is particularly useful
when you need to import CommonJS modules into an ESM
context. Here's how you can use it:
Import createRequire
:
javascriptimport { createRequire } from 'module';
Create a require
Function:
javascriptconst require = createRequire(import.meta.url);
Here, import.meta.url
provides the
URL of the current module, ensuring that the
require
function behaves as if it
were called in a CommonJS module at the same
location.
Use the require
Function:
javascriptconst commonjsModule = require('./commonjs-module');
This allows you to import CommonJS modules using
the require
function within an ESM
module.
Example:
javascript// ESM module (example.mjs) import { createRequire } from 'module'; const require = createRequire(import.meta.url); // Import a CommonJS module const fs = require('fs'); fs.readFile('./example.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });
Considerations:
Node.js Version Compatibility:
The createRequire
function was
introduced in Node.js v12.2.0. If you're working
with an earlier version, you can use the
create-require
package as a polyfill.
Dynamic Imports: While
createRequire
is useful for
synchronously importing CommonJS modules into ESM,
you can also use dynamic
import()
statements to asynchronously
load modules. However,
createRequire
provides a synchronous
alternative that can be more straightforward in
certain scenarios.
By using createRequire
, you can
seamlessly integrate CommonJS modules into your ESM
projects, maintaining compatibility across different
module systems.