What is @esbuild-kit/cjs-loader?
@esbuild-kit/cjs-loader is an npm package that allows you to use ES modules (ESM) in CommonJS (CJS) environments. It leverages esbuild to transpile ESM to CJS on the fly, enabling seamless integration of modern JavaScript modules in older Node.js projects.
What are @esbuild-kit/cjs-loader's main functionalities?
Transpile ESM to CJS
This feature allows you to transpile ES modules to CommonJS on the fly. By registering the loader, you can require ESM files in a CJS environment.
const { register } = require('@esbuild-kit/cjs-loader');
register();
const esmModule = require('./esmModule.mjs');
console.log(esmModule);
Custom Loader Options
You can customize the loader options such as the target ECMAScript version and the output format. This provides flexibility in how the ESM is transpiled to CJS.
const { register } = require('@esbuild-kit/cjs-loader');
register({ target: 'es2017', format: 'cjs' });
const esmModule = require('./esmModule.mjs');
console.log(esmModule);
Other packages similar to @esbuild-kit/cjs-loader
esm
The 'esm' package allows you to use ES modules in Node.js without the need for a build step. It provides a seamless way to load ESM in a CJS environment, similar to @esbuild-kit/cjs-loader, but without leveraging esbuild for transpilation.
babel-register
The 'babel-register' package hooks into Node.js require to transpile files on the fly using Babel. It supports a wide range of JavaScript syntax transformations, including ESM to CJS, but may have a larger performance overhead compared to @esbuild-kit/cjs-loader.
ts-node
The 'ts-node' package allows you to run TypeScript files directly in Node.js. It can also handle ESM to CJS transpilation when used with appropriate TypeScript configurations. It is more focused on TypeScript but can be used for similar purposes.
cjs-loader
Node.js require()
hook to instantaneously transform ESM & TypeScript to CommonJS on demand using esbuild.
Features
- Transforms ESM & TypeScript to CommonJS on demand
- Supports TS extensions
.cjs
& .mjs
(.cts
& .mts
) - Cached for performance boost
- Supports Node.js v12.16.2+
- Handles
node:
import prefixes - Resolves
tsconfig.json
paths
Tip:
cjs-loader doesn't hook into dynamic import()
calls.
Use this with esm-loader for import()
support. Alternatively, use tsx to handle them both automatically.
Install
npm install --save-dev @esbuild-kit/cjs-loader
Usage
Pass @esbuild/cjs-loader
into the --require
flag
node -r @esbuild/cjs-loader ./file.js
TypeScript configuration
The following properties are used from tsconfig.json
in the working directory:
jsxFactory
jsxFragmentFactory
Custom tsconfig.json
path
By default, tsconfig.json
will be detected from the current working directory.
To set a custom path, use the ESBK_TSCONFIG_PATH
environment variable:
ESBK_TSCONFIG_PATH=./path/to/tsconfig.custom.json node -r @esbuild/cjs-loader ./file.js
Cache
Modules transformations are cached in the system cache directory (TMPDIR
). Transforms are cached by content hash so duplicate dependencies are not re-transformed.
Set environment variable ESBK_DISABLE_CACHE
to a truthy value to disable the cache:
ESBK_DISABLE_CACHE=1 node -r @esbuild/cjs-loader ./file.js
Related