Skip to content

fix(cjs): require the ESM entry directly, fail clearly on Node without require(esm), and stop publishing devcheck - #58

Merged
Shinrai merged 1 commit into
nextfrom
fix/cjs-require-esm-check
Oct 3, 2026
Merged

Shinrai merged 1 commit into
nextfrom
fix/cjs-require-esm-check

Conversation

@cldmv-bot

@cldmv-bot cldmv-bot Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

🚀 What's Changed

💥 Breaking Changes

No breaking changes

✨ Features

No new features

🐛 Bug Fixes

  • fix(cjs): require the ESM entry directly, fail clearly on Node without require(esm), and stop publishing devcheck (e10d068)

📦 Dependencies

No dependency updates

🔧 Other Changes

No other changes

👥 Contributors

…t require(esm), and stop publishing devcheck

index.cjs used createRequire(__filename) to synchronously load index.mjs. That
idiom predates Node's native require(esm) and breaks bundlers (esbuild,
webpack) that don't follow a createRequire-constructed require the way they
follow a literal require() call.

- index.cjs: a plain require("./index.mjs") instead of createRequire. Where
  Node.js has no require(esm) (before 20.19 / 22.12) it now throws
  ERR_REQUIRE_ESM with a message pointing to import() instead of a bare
  loader error.
- index.mjs already avoided top-level await, so no change was needed there.
- devcheck.mjs is a source-checkout-only dev warning and was never meant to
  ship: dropped "./devcheck" from exports and devcheck.mjs/types/devcheck.d.mts
  from files, and trimmed the now-stale devcheck globs out of bundle-size.yml's
  dist_paths. devcheck.mjs itself stays in the repo (index.mjs's fire-and-forget
  import of it already tolerates a missing file in the published package).
- tests/cjs: node:test checks run by `npm test` and `npm run coverage` after
  Vitest: require() returns the same objects as import (type/identity only -
  droidsock() opens a real ADB connection, so no call is made), and the version
  check fires when require(esm) is off. test:cjs runs with CI=1 so devcheck's
  own NODE_OPTIONS guard (unrelated to this fix, source-checkout only) doesn't
  process.exit() the test runner.
@cldmv-bot cldmv-bot Bot added ! fix → next v4 flow: fix contributor PR targeting the next integration branch area: tests Touches test files, fixtures, or test infrastructure type: ci Changes to CI workflows, actions, or build pipelines type: config Changes to repository or project configuration files type: dependencies Relates to dependency updates, version bumps, or package management labels Oct 3, 2026
@Shinrai
Shinrai merged commit 93c42b6 into next Oct 3, 2026
31 checks passed
@cldmv-bot
cldmv-bot Bot deleted the fix/cjs-require-esm-check branch October 3, 2026 18:22
cldmv-bot Bot added a commit that referenced this pull request Oct 5, 2026
# DroidSock v2.0.3 Changelog

**Release Date**: October 2026
**Release Type**: Patch
**Branch**: `release/2.0.3`

---

## Overview

v2.0.3 cleans up the CommonJS entry and the published file list. `index.cjs` now loads the ESM entry with a plain `require()`, which bundlers can follow, and fails with a clear message on Node.js versions that can't `require()` ES modules. The ESM entry and the API itself are unchanged.

The release also stops publishing `devcheck.mjs`, a source-checkout-only development check, and removes its `./devcheck` subpath export. Removing an export is technically a breaking change despite this being a patch release, so it's listed under Breaking Changes below. In practice the module did nothing in an installed copy of the package.

---

## 💥 Breaking Changes

### `@cldmv/droidsock/devcheck` is no longer exported or published ([#58](#58))

`package.json` no longer lists a `./devcheck` export, and `devcheck.mjs` and `types/devcheck.d.mts` are no longer in the published files. Importing `@cldmv/droidsock/devcheck` now fails with `ERR_PACKAGE_PATH_NOT_EXPORTED`.

The module was never part of the documented API. It has no exports; its only job is to warn a developer working in a source checkout who forgot to set `NODE_OPTIONS=--conditions=droidsock-dev`. It only acts when a `src/` folder sits next to it, and the published package has no `src/`, so importing it from an installed copy did nothing. `index.mjs` still loads it fire-and-forget and already tolerates it being missing, so the main entry is unaffected.

**Upgrade step:** if anything imports `@cldmv/droidsock/devcheck`, delete that import. Nothing replaces it, because it never did anything outside this repository.

## 🐛 Bug Fixes

### Require the ESM entry directly, and fail clearly without `require(esm)` ([#58](#58))

`index.cjs` used `createRequire(__filename)` to load `index.mjs`. That idiom predates Node's native `require(esm)`, and bundlers such as esbuild and webpack don't follow a `createRequire`-constructed `require` the way they follow a literal `require()` call, so a CommonJS build that bundled droidsock could miss the ESM entry.

- `index.cjs` now calls `require("./index.mjs")` directly and exports the same functions as before (`module.exports` is the `droidsock` quick path, with `createDroidSock`, `DroidSock`, `ADB` and `AndroidDebugBridge` as properties).
- On a Node.js version without `require(esm)` (before 20.19.0, or 22.0.0 to 22.11.x), `index.cjs` throws an `ERR_REQUIRE_ESM` error whose message names the supported versions and points to `import()`, instead of a bare loader error. The package's `engines.node` is already `>=22.12.0`, so this only matters for installs that ignore `engines`.
- `index.mjs` already avoided top-level `await`, so it needed no change.
- New `tests/cjs/entry.test.cjs` checks run under Node's own test runner after Vitest, from both `npm test` and `npm run coverage` (through the new `test:cjs` script). They check that `require()` returns the same functions as `import`, and that the version check fires when `require(esm)` is turned off. They don't call `droidsock()`, since that opens a real ADB connection.

## 🔧 CI & tooling

- `bundle-size.yml` no longer lists the `devcheck` files in `dist_paths`, matching the new published file list ([#58](#58)).

## 📚 Documentation

- **NEW:** [docs/changelog/v2/v2.0.3.md](./v2.0.3.md): this changelog.
- **NEW:** backfilled [v1.1.1](../v1/v1.1.1.md), [v2.0.1](./v2.0.1.md) and [v2.0.2](./v2.0.2.md).
- README restructured to the standard CLDMV layout, with a new Requirements section that states the Node.js floor for `import` and `require()`.

## 🔧 Dependencies

Both changes are to development dependencies; the package has no runtime dependency changes and the published package is unaffected. Only `package.json` and the lockfile changed in these bumps, and no file headers were restamped.

- `@cldmv/fix-headers` `^2.1.2` → `^2.2.0` ([#61](#61) took it to `^2.1.4`, [#63](#63) to `^2.2.0`). Version 2.1.4 no longer writes a JavaScript comment into JSON or Markdown files, processes every repeated `--input`, and never walks dependency folders such as `node_modules`. Version 2.2.0 makes the `@Last modified by` header tag follow content edits only, so a header-only rewrite keeps the recorded editor instead of replacing it. It requires Node.js `>=22.12.0`, which matches the package's `engines.node`.
- `@cldmv/configs` `^1.2.1` → `^1.2.4` ([#63](#63)). It provides the shared `fix-headers` configuration that `.configs/fix-headers.json` extends. Version 1.2.4 turns off `forceAuthorUpdate` and `forceLastModifiedAuthorUpdate` (both were on in 1.2.1), so the shared configuration no longer overwrites the recorded author or last editor.

---

## Upgrade notes

- If anything imports `@cldmv/droidsock/devcheck`, remove that import. Nothing else needs to change.
- `import droidsock from "@cldmv/droidsock"` and `require("@cldmv/droidsock")` both work as before on Node.js 22.12.0 or later.



<details>
<summary>👥 Contributors</summary>

- @Shinrai

</details>

---

<!-- coverage-start -->

![coverage](https://img.shields.io/badge/coverage-91.8%25-brightgreen?style=for-the-badge&logo=vitest&logoColor=white)

| Metric | Coverage |
|--------|----------|
| Statements | 93.0% |
| Branches   | 85.3% |
| Functions  | 94.4% |
| Lines      | 94.5% |

*Avg: **91.8%** · `d7d1cb5` · Node lts/**

<!-- coverage-end -->

<!-- co-authors -->

Co-authored-by: Shinrai <Shinrai@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: tests Touches test files, fixtures, or test infrastructure ! fix → next v4 flow: fix contributor PR targeting the next integration branch type: ci Changes to CI workflows, actions, or build pipelines type: config Changes to repository or project configuration files type: dependencies Relates to dependency updates, version bumps, or package management

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant