Skip to content

fix: let ES modules on Node 22 import { Adb } and the other classes - #586

Merged
koral-- merged 2 commits into
masterfrom
fix/esm-named-exports
Nov 7, 2026
Merged

koral-- merged 2 commits into
masterfrom
fix/esm-named-exports

Conversation

@koral--

@koral-- koral-- commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Problem

Every README example did import Adb from '@devicefarmer/adbkit'. The package is CommonJS (exports.default = Adb plus __esModule), and Node's ES module loader always makes a CommonJS module's default import the whole module.exports. So Adb.createClient is undefined and you get "TypeError: Adb.createClient is not a function" (#432).

The obvious README fix, import { Adb } from '@devicefarmer/adbkit', doesn't work on Node 22 either, which is the minimum this package supports:

SyntaxError: Named export 'Adb' not found. The requested module '@devicefarmer/adbkit' is a CommonJS module, which may not support all module.exports as named exports.

Node finds a CommonJS module's named exports by scanning its source (cjs-module-lexer). index.ts re-exported the classes with export { default as Adb } from './src/adb', and TypeScript compiles that to getters such as get: function () { return __importDefault(adb_2).default; }. Node 22's lexer doesn't recognize these, so on Node 22 the only named export was KeyCodes. Newer Node versions (24 and 26 here) do find them.

Fix

  • index.ts imports Adb, Parser, Client, DeviceClient and ShellCommand and then exports them. That compiles to exports.Adb = adb_1.default assignments, which every Node version detects. The CommonJS module object has the same keys and values, the default export is still Adb, and the declarations export the same names and types. require(), TypeScript consumers and stf's import adbkit from '@devicefarmer/adbkit' (which uses adbkit.Adb, adbkit.KeyCodes and adbkit.Parser) keep working.
  • README: every example uses import { Adb } from '@devicefarmer/adbkit', and Getting started shows the ESM and CommonJS forms and explains why the default import doesn't work from Node ESM.
  • test/exports.mts: a dynamic import() of the package gives the named exports (Adb, Parser, Client, DeviceClient, ShellCommand and KeyCodes), equal to require()'s. CI runs it on Node 22 and 24.

Why not make the default import work instead: Node ESM always maps a CommonJS default to module.exports. Making that default Adb would take an ESM entry point (a dual package), which changes what existing default imports get. stf, for example, relies on the default import being the module object. It would also bring the dual-package hazard (two copies of Parser and its error classes). The named export is the form that works everywhere.

The Chrome ADB extension removal (#573) is in a separate PR (#585).

Fixes #432

Verification

The new test, Node 22.22.2: fails on master (TypeError: Cannot read properties of undefined (reading 'createClient')) and passes with this change. npm test on Node 22.22.2 and 26.13.0: 285 passing. npm run lint is clean.

A scratch consumer that installed the packed tarball:

master, Node 22 master, Node 26 this branch, Node 22 this branch, Node 26
.mjs, import { Adb } SyntaxError: Named export 'Adb' not found works works works
.cjs, const { Adb } = require() works works works works
.mjs, import Adb (old README) createClient undefined undefined undefined undefined
TS 6, nodenext, strict, skipLibCheck: false, import { Adb }, run on Node 22 compiles, fails at runtime compiles and runs

In stf (master) with this tarball, on Node 22 and 26, import adbkit from '@devicefarmer/adbkit' still gives Adb, Parser, Client, DeviceClient, ShellCommand, KeyCodes, default, with adbkit.Adb.util.readAll, adbkit.KeyCodes.KEYCODE_HOME === 3 and adbkit.Parser.FailError. lib/util/adbutil.js loads, and tsc -p . --noEmit passes.

Review follow-up (commit cb490d7)

The README's device.shell() examples were Bluebird-era and couldn't run: they used Promise.map(), redeclared device inside its own initializer (a TDZ error), and called an undefined adb.util.readAll. Both now use async/await with Promise.all(devices.map(...)) and Adb.util.readAll(). The logcat one waits for each connection to close before it logs "Done.". The "List files" example now calls device.readdir(); client.readdir(serial, …) doesn't exist.

I ran each rewritten example from the README text against this branch's build on the API 30 and API 36 emulators. All ran and left no sockets open:

  • echo $RANDOM printed a number per device and "Done." (also on Node 22);
  • the logcat example read 100 chunks per device and closed, and no logcat process was left on either device;
  • readdir finished.

Not touched: the TCP/devtools example (client.tcpip() section) also redeclares device and uses an undefined id. It needs a device with a DHCP address, which I couldn't run here. The shell() API docs still list a callback(err, conn) that v4 doesn't take.

🤖 Generated with Claude Code

koral-- and others added 2 commits November 7, 2026 01:28
The README imported the default export. The package is CommonJS, so in
a Node.js ES module the default import is the whole module object and
Adb.createClient is undefined (#432). The named import didn't work
either on Node 22: index.ts re-exported the classes with
`export { default as Adb } from`, which TypeScript compiles to getters
that call __importDefault(). Node 22's cjs-module-lexer doesn't
recognize those, so only KeyCodes was found and
`import { Adb } from '@devicefarmer/adbkit'` failed with "Named export
'Adb' not found". Newer Node versions detect them.

index.ts now imports the classes and then exports them, which compiles
to plain `exports.Adb = adb_1.default` assignments that every Node
version detects. The module object, the default export and the
declarations stay the same, so `require()`, TypeScript and stf's
`import adbkit from` keep working. The README examples use
`import { Adb }`, and Getting started explains why the default import
doesn't work from Node ES modules.

Fixes #432

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Karol Wrótniak <[email protected]>
The device.shell() examples used Bluebird's Promise.map(), redeclared
`device` inside its own initializer, and called an undefined
adb.util.readAll. They now use Promise.all() over async functions and
Adb.util.readAll(). The logcat example waits for each connection to
close before it logs "Done.". The readdir example calls
device.readdir(), as the client has no readdir() of its own.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Karol Wrótniak <[email protected]>
@koral--
koral-- merged commit 50e8f86 into master Nov 7, 2026
6 checks passed
@koral--
koral-- deleted the fix/esm-named-exports branch November 7, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do anyone get the same error? "TypeError: Adb.createClient is not a function"

1 participant