CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-std--esm

Enable ES modules in Node.js 6+ environments with zero dependencies and full CommonJS compatibility

Pending
Overview
Eval results
Files

module-bridge.mddocs/

Module Bridge

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.

Capabilities

Main Export Function

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;

REPL Integration

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 work

CLI Integration

Command-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.mjs

Test Framework Integration

Integration 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"

Context-Aware Behavior

The module bridge automatically detects execution context and adapts its behavior:

  • CJS Bridge: When used with module parameter, creates enhanced require
  • CLI: When preloaded with -r, enables ESM for the main script
  • REPL: When used in REPL, enables ESM and returns status message
  • Eval: When used in eval context, provides appropriate ESM support

Performance Features

  • Bytecode Caching: Compiled modules are cached to .cache directory for faster subsequent loads
  • Lazy Loading: Hooks are applied only when needed to minimize startup overhead
  • V8 Optimization: Uses V8 script caching for optimal performance

Install with Tessl CLI

npx tessl i tessl/npm-std--esm

docs

configuration.md

index.md

module-bridge.md

tile.json