TypeScript
zx is written in TypeScript and provides the corresponding libdefs out of the box. Types are TS 4+ compatible. Write code in any suitable format .ts
, .mts
, .cts
or add a custom loader.
ts
// script.ts
import { $ } from 'zx'
const list = await $`ls -la`
Some runtimes like Bun or Deno have built-in TS support. Node.js requires additional setup. Configure your project according to the ES modules contract:
- Set
"type": "module"
in package.json - Set
"module": "ESNext"
in tsconfig.json.
Using TypeScript compiler is the most straightforward way, but native TS support from runtimes is gradually increasing.
bash
# Since Node.js v22.6.0
node --experimental-strip-types script.js
bash
# Since Node.js v22.6.0
NODE_OPTIONS="--experimental-strip-types" zx script.js
bash
npm install typescript
tsc script.ts
node script.js
bash
npm install ts-node
ts-node script.ts
# or via node loader
node --loader ts-node/esm script.ts
bash
npm install swc-node
swc-node script.ts
bash
npm install tsx
tsx script.ts
# or
node --import=tsx script.ts
bash
bun script.ts
bash
deno run --allow-read --allow-sys --allow-env --allow-run script.ts