Skip to content

package-layout: one generic CJS shim that returns the right shape for any package #589

Done

Description

@Shinrai

src/index.cjs should be the same file in every repo, a drop-in that works out the right shape itself instead of each repo hand-writing .default unwraps and attached aliases (which drift from the ESM exports):

"use strict";

if (!process.features?.require_module) {
	const error = new Error(`<pkg>: require() needs Node.js ^20.19.0 or >=22.13.0 (this is ${process.version}). On older Node.js, load the package with import() instead.`);
	error.code = "ERR_REQUIRE_ESM";
	throw error;
}

// Same exports as `import`. One distinct exported value (aliases of it count once) comes back as that value,
// so require(x)(...) works; anything else comes back as the module object ({ default, ...named }).
const esm = require("./index.mjs");
const values = new Set(Object.keys(esm).filter((key) => key !== "__esModule").map((key) => esm[key]));
module.exports = values.size === 1 ? values.values().next().value : esm;

Only the package name in the message and the Node floor change per repo. The same pattern applies to other CJS entries (e.g. src/drizzle.cjs → ./drizzle.mjs).

Behaviour, checked by running it against the published packages:

Package Generic shim returns Today
fix-headers, stubborn-tcp, sizeofvar, mxnet-control, droidsock, prettier-plugin-jsonv the single value same
sqlite-wasm, vitest-runner (and the other plain require("./index.mjs") repos) module object same
uuid, wisp, node-android-tv-remote, holdmytask module object default only: changes, a major each
eslint-plugin-jsonv module object the plugin object: changes, check first

For the four that change, require(x)(…) callers break and need require(x).default (or the named export), so each repo adopts the shim in its next major.

eslint-plugin-jsonv exports the plugin plus JsonvSourceCode. ESLint's legacy config loader require()s a plugin and may expect the plugin object itself. Check whether it unwraps .default; if it doesn't, that repo keeps a plugin-specific shim as the one documented exception.

To do: put the shim in docs/conventions/package-layout.md (same file as #582) and the example template, then file one issue per repo to adopt it (all repos with a src/index.cjs; it's a no-op change for every repo that already returns the same shape).

Activity

  1. added
    type: enhancement
    priority: medium
    status: not started
    on Nov 26, 2026
  2. added a commit that references this issue on Nov 26, 2026
    5341762
  3. added
    status: in progress
    and removed
    status: not started
    on Nov 26, 2026
  4. Shinrai commented on Nov 26, 2026

    @Shinrai
    ContributorAuthor

    Refined rule: if the ESM entry exports one distinct value (aliases like default + fixHeaders for the same function count as one), src/index.cjs may keep returning that value, so require(x)(…) keeps working. If it exports more than one distinct value, src/index.cjs is exactly module.exports = require("./index.mjs");. require() then returns the module object ({ __esModule: true, default, …named }): require(x).default, require(x).named and const { default: main, named } = require(x) all work, the same as import * as.

    Checked against the published packages (distinct values in the ESM namespace):

    • One value, keep as is: fix-headers, stubborn-tcp, sizeofvar, mxnet-control, droidsock, prettier-plugin-jsonv.
    • More than one, change to pass the module object through (a major each):
      • uuid: UUID and ISSUER_CATEGORIES
      • wisp: wisp and wispSync
      • node-android-tv-remote: createRemote and createAndroidTVRemote
      • holdmytask: 5 distinct values under 12 names
    • More than one, but check the host first: eslint-plugin-jsonv (default is the plugin, plus JsonvSourceCode). ESLint's legacy config loader require()s the plugin and expects the plugin object, so this may stay an exception.

    So the per-repo issues are for uuid, wisp, node-android-tv-remote and holdmytask (plus a look at eslint-plugin-jsonv).

  5. changed the title [-]package-layout: the CJS wrapper must return the same module as ESM, never an unwrapped default[/-] [+]package-layout: one generic CJS shim that returns the right shape for any package[/+] on Nov 26, 2026
  6. added
    status: implemented
    and removed
    status: in progress
    on Nov 26, 2026
  7. cldmv-bot commented on Nov 26, 2026

    @cldmv-bot
    Contributor

    Marked status: implemented — resolved by #584, merged into next. This issue closes automatically when that work ships to the default branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: mediumstatus: implementedtype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions