Node esm patterns

From Intense-Visions/harness-engineering · View interactive version

Node esm patterns is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Write Node.js ES modules correctly using import.meta.url, package.json type, and CJS interop

How to use this skill

Skill file

Node.js ESM Patterns

Write Node.js ES modules correctly using import.meta.url, package.json type, and CJS interop

When to Use

Instructions

  1. Enable ESM in package.json:
{ "type": "module" }

All .js files are now ESM. Use .cjs extension for CommonJS files.

  1. Replace __dirname and __filename:
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const configPath = join(__dirname, 'config.json');
  1. Dynamic imports:
const { default: chalk } = await import('chalk');

// Conditional imports
const db =
  process.env.DB === 'postgres' ? await import('./db/postgres.js') : await import('./db/sqlite.js');
  1. Import JSON with import assertions:
import config from './config.json' with { type: 'json' };
// Or dynamically
const pkg = await import('./package.json', { with: { type: 'json' } });
  1. Import CommonJS modules from ESM:
// Default import for CJS modules
import express from 'express';

// Named imports may not work for all CJS modules
// Use default import + destructure instead
import pkg from 'lodash';
const { pick, omit } = pkg;
  1. Top-level await:
// Works at module top level in ESM (not in CJS)
const config = await loadConfig();
const db = await connectDatabase(config.dbUrl);

export { db, config };
  1. File extensions are required in imports:
// CJS: works without extension
const { foo } = require('./utils');

// ESM: extension required
import { foo } from './utils.js'; // .js even for .ts files with Node16 module resolution
  1. Configure TypeScript for ESM:
{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "ES2022"
  }
}

Details

ES modules are the standard module system for JavaScript. Node.js supports them natively since v12 (stable since v14). ESM is statically analyzable, supports tree-shaking, and uses import/export syntax.

ESM vs CJS differences:

import.meta: ESM-only global with module metadata:

Dual package publishing: Libraries can support both ESM and CJS consumers:

{
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.cjs"
    }
  }
}

Trade-offs:

Source

https://nodejs.org/api/esm.html

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.