tinyexec is a lightweight Node.js library designed for executing child processes, offering a streamlined, promise-based API as an alternative to Node.js's native `child_process` module or more feature-rich libraries like `execa`. It abstracts away direct stream manipulation, providing a simpler interface for spawning, piping, and awaiting process results. As of its current stable version, 1.1.1, it supports both asynchronous (`x`) and synchronous (`xSync`) command execution, including options for setting timeouts, integrating `AbortSignal` for cancellation, and passing `stdin` input. Key differentiators include its focus on minimalism, automatic resolution of local `node_modules` binaries, and the ability to iterate over process output lines asynchronously. The package is actively maintained with frequent minor releases and bug fixes, and it is ESM-only since version 1.0.0, requiring Node.js 18 or higher.
npm install tinyexecVerified import paths — ran on the pinned version, not inferred.
Demonstrates asynchronous (`x`) and synchronous (`xSync`) command execution, including options, piping, and async iteration over output lines. It also shows optional use of `args-tokenizer` for command string parsing.
Migrate your project to use ECMAScript Modules (ESM) by setting `"type": "module"` in your `package.json` or by using `.mjs` file extensions. Update all `require('tinyexec')` calls to `import { x } from 'tinyexec';`.To ensure consistent error handling, always explicitly set the `throwOnError` option in your calls to `x` or `xSync` (e.g., `{ throwOnError: true }` to throw on non-zero exit codes, or `{ throwOnError: false }` to always return the result object).If trimmed output is desired, explicitly clean the string: `result.stdout.replace(/\r?\n$/, '')`.
Ensure your Node.js runtime environment is version 18 or newer. Update Node.js using your preferred package manager (e.g., `nvm install 18` or `nvm use 18`).
Split the command string into a command and an array of arguments, e.g., `await x('echo', ['Hello, World!'])`. For parsing complex shell syntax, consider using `args-tokenizer`: `const [cmd, ...args] = tokenizeArgs(commandString); await x(cmd, args);`Refactor your code to use ES Module `import` syntax: `import { x } from 'tinyexec';`. Ensure your project's `package.json` includes `"type": "module"` or that your file has a `.mjs` extension.Inspect the `stderr` output (e.g., `error.stderr`) for clues about why the command failed. If a non-zero exit code should not throw an error, set `throwOnError: false` in the options object.
Chain the `.pipe()` call directly onto the `x()` invocation before awaiting: `const proc1 = x('ls', ['-l']); const proc2 = proc1.pipe('grep', ['.js']); const result = await proc2;`