Repository navigation
fix: let ES modules on Node 22 import { Adb } and the other classes - #586
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Every README example did
import Adb from '@devicefarmer/adbkit'. The package is CommonJS (exports.default = Adbplus__esModule), and Node's ES module loader always makes a CommonJS module's default import the wholemodule.exports. SoAdb.createClientisundefinedand 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:Node finds a CommonJS module's named exports by scanning its source (cjs-module-lexer).
index.tsre-exported the classes withexport { default as Adb } from './src/adb', and TypeScript compiles that to getters such asget: function () { return __importDefault(adb_2).default; }. Node 22's lexer doesn't recognize these, so on Node 22 the only named export wasKeyCodes. Newer Node versions (24 and 26 here) do find them.Fix
index.tsimportsAdb,Parser,Client,DeviceClientandShellCommandand then exports them. That compiles toexports.Adb = adb_1.defaultassignments, which every Node version detects. The CommonJS module object has the same keys and values, the default export is stillAdb, and the declarations export the same names and types.require(), TypeScript consumers and stf'simport adbkit from '@devicefarmer/adbkit'(which usesadbkit.Adb,adbkit.KeyCodesandadbkit.Parser) keep working.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 dynamicimport()of the package gives the named exports (Adb,Parser,Client,DeviceClient,ShellCommandandKeyCodes), equal torequire()'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 defaultAdbwould 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 ofParserand 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 teston Node 22.22.2 and 26.13.0: 285 passing.npm run lintis clean.A scratch consumer that installed the packed tarball:
.mjs,import { Adb }.cjs,const { Adb } = require().mjs,import Adb(old README)createClientundefinednodenext, strict,skipLibCheck: false,import { Adb }, run on Node 22In stf (master) with this tarball, on Node 22 and 26,
import adbkit from '@devicefarmer/adbkit'still givesAdb, Parser, Client, DeviceClient, ShellCommand, KeyCodes, default, withadbkit.Adb.util.readAll,adbkit.KeyCodes.KEYCODE_HOME === 3andadbkit.Parser.FailError.lib/util/adbutil.jsloads, andtsc -p . --noEmitpasses.Review follow-up (commit
cb490d7)The README's
device.shell()examples were Bluebird-era and couldn't run: they usedPromise.map(), redeclareddeviceinside its own initializer (a TDZ error), and called an undefinedadb.util.readAll. Both now use async/await withPromise.all(devices.map(...))andAdb.util.readAll(). The logcat one waits for each connection to close before it logs "Done.". The "List files" example now callsdevice.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 $RANDOMprinted a number per device and "Done." (also on Node 22);logcatprocess was left on either device;Not touched: the TCP/devtools example (
client.tcpip()section) also redeclaresdeviceand uses an undefinedid. It needs a device with a DHCP address, which I couldn't run here. Theshell()API docs still list acallback(err, conn)that v4 doesn't take.🤖 Generated with Claude Code