Skip to content

feat!: rewrite as ES modules, with TypeScript types - #208

Merged
satazor merged 3 commits into
masterfrom
feat/esm
Nov 29, 2026
Merged

satazor merged 3 commits into
masterfrom
feat/esm

Conversation

@satazor

@satazor satazor commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Rewrites cross-spawn as an ES module for v8, with named exports only, like cross-spawn-esm, and ships TypeScript types.

Commits

  1. refactor: move the source code to src/: lib/ becomes src/, and index.js moves to src/index.js.
  2. feat!: rewrite as ES modules:
    • "type": "module", and an exports map with only . and ./package.json.
    • Named exports only: spawn and spawnSync (was spawn.sync), no default export, including internal modules. readShebang.js and resolveCommand.js are renamed to shebang.js and command.js.
    • path-key is imported normally, instead of require('path-key').default.
    • README: usage updated.
  3. feat: ship TypeScript types:
    • src/index.d.ts: spawn and spawnSync typed as Node's own (typeof childProcess.spawn/spawnSync), plus SpawnOptions and SpawnSyncOptions.
    • Type tests in test/types/ (ESM .mts and CommonJS .cts), checked with npm run typecheck (TypeScript 7, @types/node 22 for the minimum Node.js version), which CI runs with the lint. They include exact type comparisons with Node's types, and @ts-expect-error checks, e.g. that there's no default export and that spawn.sync() is gone. Breaking the types on purpose makes them fail.

CommonJS

const { spawn, spawnSync } = require('cross-spawn') keeps working, since Node.js 22.13+ can require() ES modules without a warning. New tests check, by package name, that the package can be imported from ESM, required from CommonJS without warnings, and that internal files are not exported. The worker thread tests still require() the package.

Known costs of ESM-only, checked separately:

  • Jest 30.5 can't load an ES module required from CommonJS tests unless running on Node.js 24.9+ with --experimental-vm-modules, or with a transform. jest.mock() with a factory still works.
  • What require() returns is a read-only module namespace, so stubbing require('cross-spawn').spawn no longer works.
  • TypeScript CommonJS projects using module: nodenext need TypeScript 5.8+ to import it.

BREAKING CHANGES

  • No default export, and spawn.sync() is now spawnSync().
  • Internal files, such as cross-spawn/lib/parse, can't be imported anymore.

The entry point moves from `index.js` to `src/index.js`, next to the rest
of the code that was in `lib/`.
cross-spawn is now an ES module with named exports only, `spawn` and
`spawnSync`, like cross-spawn-esm. CommonJS code can still load it with
`require()`, which Node.js 22.13 and later support without a warning.

The package now has an `exports` map, so only the package itself and its
`package.json` can be imported. Internal modules use named exports too,
and `readShebang.js` and `resolveCommand.js` are renamed to `shebang.js`
and `command.js`.

BREAKING CHANGE: there's no default export anymore, and `spawn.sync()` is
now `spawnSync()`. Use `import { spawn, spawnSync } from 'cross-spawn'` or
`const { spawn, spawnSync } = require('cross-spawn')`. Internal files, such
as `cross-spawn/lib/parse`, can't be imported anymore.
`spawn` and `spawnSync` have the same types as Node's, so
`@types/cross-spawn` isn't needed anymore. `SpawnOptions` and
`SpawnSyncOptions` are exported too, like cross-spawn-esm does.

The types are checked with `npm run typecheck`, which compiles type tests
for ES modules and CommonJS with TypeScript, including exact comparisons
with Node's types. CI runs it with the lint.
@satazor
satazor merged commit 4b27af5 into master Nov 29, 2026
8 checks passed
@satazor
satazor deleted the feat/esm branch November 29, 2026 05:00
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.

1 participant