Enable ES modules in Node.js 6+ environments with zero dependencies and full CommonJS compatibility
—
The module bridge is the core functionality of @std/esm that creates a CommonJS bridge enabling ES module syntax in Node.js environments. This is the primary interface that most developers will use.
The @std/esm package exports a single function that creates an enhanced require function with ES module support.
/**
* Main export function that enables ES module support
* @param {Object} module - The module object (typically 'module')
* @param {Object} [options] - Configuration options
* @returns {Function} Enhanced require function with ESM capabilities
*/
require("@std/esm")(module, options)Usage Examples:
// Basic usage - enables .mjs files
require = require("@std/esm")(module);
// With options - enable .js files with ESM syntax
require = require("@std/esm")(module, { mode: "js" });
// Full CJS bridge pattern
require = require("@std/esm")(module, { cjs: true, mode: "js" });
module.exports = require("./main.mjs").default;Special behavior when used in Node.js REPL environment.
/**
* REPL integration that returns a status message
* @returns String message indicating @std/esm is enabled
*/
require("@std/esm"): "@std/esm enabled";Usage Examples:
$ node -r @std/esm
> import { readFile } from 'fs'; // ES modules work directly
$ node
> require("@std/esm")
@std/esm enabled
> import { readFile } from 'fs'; // ES modules now workCommand-line integration using Node.js -r flag for preloading.
Usage Examples:
# Enable ESM for a specific file
node -r @std/esm main.mjs
# Works with any Node.js CLI options
node -r @std/esm --inspect main.mjs
# Can be combined with other preloaded modules
node -r @std/esm -r other-module main.mjsIntegration patterns for popular Node.js test frameworks.
Usage Examples:
// AVA configuration (package.json)
{
"ava": {
"require": ["@std/esm"]
}
}
// Mocha usage
mocha -r @std/esm test/**/*.mjs
// NYC coverage
nyc --require @std/esm mocha test/**/*.mjs
// Node-tap
node-tap --node-arg=-r --node-arg=@std/esm test/**/*.mjs
// PM2 process manager
pm2 start app.mjs --node-args="-r @std/esm"The module bridge automatically detects execution context and adapts its behavior:
module parameter, creates enhanced require-r, enables ESM for the main script.cache directory for faster subsequent loadsInstall with Tessl CLI
npx tessl i tessl/npm-std--esm