Skip to content

Node.js API

In addition to the CLI, pkg exposes a small programmatic API so you can drive builds from a Node.js script — useful for custom release pipelines, CI integration, or wrapping pkg inside a bigger build tool.

Install

sh
npm install --save-dev @yao-pkg/pkg

Import

js
const { exec } = require('@yao-pkg/pkg');
js
import { exec } from '@yao-pkg/pkg';

exec()

exec() accepts either a CLI-style argv array or a typed options object, and returns a Promise<void> that resolves when the build is complete, or rejects on failure.

ts
import { exec, PkgExecOptions } from '@yao-pkg/pkg';

await exec({
  input: 'index.js',
  targets: ['node22-linux-x64'],
  output: 'dist/app',
  compress: 'Brotli',
});

Only input is required. Everything else mirrors the CLI flags — see the full field list below.

Argv array

js
const { exec } = require('@yao-pkg/pkg');

await exec(['index.js', '--target', 'host', '--output', 'dist/app']);

The strings are exactly what you'd pass on the command line — see Getting started → CLI reference.

PkgExecOptions fields

FieldTypeCLI equivalentNotes
inputstringpositional <input>Required. Entry file or directory.
targetsstring[]--targetse.g. ['host'] or ['node22-linux-x64', ...].
configstring--configPath to package.json or standalone config JSON.
outputstring--outputOutput file name or template.
outputPathstring--out-pathOutput directory (mutually exclusive with output).
compress'None' | 'Brotli' | 'GZip' | 'Zstd'--compressDefault 'None'.
seaboolean--seaUse Single Executable Application mode.
bakeOptionsstring | string[]--optionsNode/V8 flags baked into the binary (e.g. ['expose-gc']).
debugboolean--debugVerbose packaging logs.
buildboolean--buildBuild base binaries from source.
bytecodeboolean--no-bytecodeDefault true. Set false to ship plain JS.
nativeBuildboolean--no-native-buildDefault true.
fallbackToSourceboolean--fallback-to-sourceShip source when bytecode compile fails.
publicboolean--publicTop-level project is public.
publicPackagesstring[]--public-packagesUse ['*'] for all.
noDictionarystring[]--no-dictUse ['*'] to disable all dictionaries.
signatureboolean--no-signatureDefault true (macOS signing when applicable).

Build a full release pipeline

js
const { exec } = require('@yao-pkg/pkg');
const { mkdir, rm } = require('node:fs/promises');
const path = require('node:path');

const DIST = 'dist';
const TARGETS = [
  'node22-linux-x64',
  'node22-linux-arm64',
  'node22-macos-arm64',
  'node22-win-x64',
];

async function build() {
  await rm(DIST, { recursive: true, force: true });
  await mkdir(DIST, { recursive: true });

  await exec({
    input: '.',
    targets: TARGETS,
    outputPath: DIST,
    compress: 'Brotli',
  });

  console.log(`built ${TARGETS.length} binaries into ${path.resolve(DIST)}`);
}

build().catch((err) => {
  console.error(err);
  process.exit(1);
});

Error handling

exec rejects with an Error whose message contains the same diagnostic you'd see on the CLI. Wrap in try/catch (or chain .catch) if you need to react to specific failures:

js
try {
  await exec(['bad/input.js']);
} catch (err) {
  console.error('pkg build failed:', err.message);
  process.exitCode = 1;
}

See also

Released under the MIT License.