Index of every page on this site: llms.txt
All skills
jasnell avatar

/node-modules-loading

@5f83e6c
Run it yourself

Node.js now runs TypeScript .ts files directly (type stripping is Stable) and loads code differently: require() of ES modules, loader hooks with module.registerHooks (which replaces module.register and --loader), package maps and the compile cache. Use when running TypeScript without a build step or instead of tsx/ts-node, requiring ESM from CommonJS, writing or migrating loader hooks, fixing "bad option: --loader", resolving packages in a monorepo, or using --config-file. Covers --no-strip-types, ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX, ERR_REQUIRE_ASYNC_MODULE, import with type 'text', Wasm import source, the "worker" condition, "private" packages and the compile cache. Node.js 24+.

Use this Skill: https://skilld.dev/gh/jasnell/node-skills/node-modules-loading

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

SKILL.md

≈177 tokens for metadata: the name and description. ≈942 when used: this file. ≈6.3k more on demand in 6 files.

Description uses 8.9% of example budget

Before choosing a Skill, your Agent reads its name and description. All available Skills share that space.

  • A shorter description leaves more room for other Skills. This entry exceeds our 1% size suggestion.

In our Claude Code example, all Skill names and descriptions share 8,000 characters. This Skill uses ≈710 characters, or 8.9%.

The 1% threshold is a size suggestion. Longer descriptions can still fit.

Your model, settings, and other Skills decide how much text your Agent can read.

Example settings and source

The example uses a 200k-token context and default Claude Code settings. The count includes the name, description, separators, and when_to_use when present. Codex also counts local file paths.

Skit's source and limits: Codex 0.160.1, Claude Code 2.1.292.

Module loading: TypeScript, require(esm), hooks, package maps

Feature Versions Stability Flag
Type stripping, on by default Stable v25.2.0, v24.12.0 2 off: --no-strip-types
require(esm) Stable v25.4.0, v24.15.0 2 off: --no-require-module
ERR_REQUIRE_ASYNC_MODULE .requireStack v26.5.0, v24.20.0 2 locations: --experimental-print-required-tla
module.registerHooks() v23.5.0, v22.15.0 (RC v25.4.0, v24.13.1) 1.2 —
module.register(), --loader removed on main (DEP0205) EOL —
Package maps v26.4.0, v24.20.0 1 --experimental-package-map=<file>
with { type: 'text' } v26.5.0, v24.19.0 1.0 --experimental-import-text
TS in "private": true deps v27.0.0-alpha.0.0.0 1 --experimental-strip-private-modules
"worker" export condition main only (verified) 1 —
Wasm import source v24.0.0 (no flag from v24.5.0) 1.2 —
Compile cache portable / readOnly v25.0.0, v24.12.0 / v26.8.0 2 NODE_COMPILE_CACHE*
--config-file v26.12.0 (v24: --experimental-config-file) 2 —

Experimental API. Before relying on these details, run the node-experimental-check skill's probe (or node --version plus a feature-detect one-liner) and check the docs for the installed version: https://nodejs.org/docs/<version>/api/<module>.html. If the installed version disagrees with this skill, the installed version wins.

Run TypeScript directly

// node app.ts   (no flag, no tsx)
import type { User } from './types.ts';   // `type` is required; the extension too
import { load } from './load.ts';

Only erasable syntax runs. enum, runtime namespace and parameter properties throw ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX. Set tsconfig erasableSyntaxOnly.

Loader hooks (sync, in-thread, for import and require)

import { registerHooks } from 'node:module';
const hooks = registerHooks({
  resolve: (s, ctx, next) => s === 'virtual:x' ? { url: s, shortCircuit: true } : next(s, ctx),
  load: (url, ctx, next) => url === 'virtual:x'
    ? { format: 'module', source: 'export default 1', shortCircuit: true } : next(url, ctx),
});
hooks.deregister();

Preload with node --import ./hooks.mjs app.js.

require(esm)

require('./x.mjs') returns the namespace. If the graph has top-level await it throws ERR_REQUIRE_ASYNC_MODULE; use await import() instead. export { X as 'module.exports' } makes require() return X.

Pitfalls

  • When a load hook edits nextLoad() output, write { ...result, source }. The docs' { source, ...result } keeps the old source.
  • A set NODE_OPTIONS env var, even an empty one, makes --config-file ignore all of the file's nodeOptions.
  • .ts under node_modules is refused. tsconfig paths is ignored; use "imports" (#x).

Don't

  • Don't use --experimental-transform-types or mode: 'transform'. Both were removed in v26.0.0.
  • Don't use module.register() or --[experimental-]loader. They are removed on main.
  • Don't add --experimental-strip-types. Type stripping is on by default.

Reference

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts5d3 checks · Risk SAFE
  • Gen Agent Trust Hub5d

    This skill provides technical documentation and examples for modern Node.js module loading features. It includes scripts to verify feature behavior and contains no malicious patterns.

  • Socket5d

    No alerts

  • Snyk5d

    Risk: LOW · No issues

Signed by skilld at 5f83e6c. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 5 days ago.

Activeupdated last week

README badge

README badge for jasnell/node-skills/node-modules-loading