// runner.jsimport { createSyncFn } from 'synckit'// the worker path must be absoluteconst syncFn = createSyncFn(require.resolve('./worker'), { tsRunner: 'tsx', // optional, can be `'node' | 'ts-node' | 'esbuild-register' | 'esbuild-runner' | 'tsx'`})// do whatever you want, you will get the result synchronously!const result = syncFn(...args)
// worker.jsimport { runAsWorker } from 'synckit'runAsWorker(async (...args) => { // do expensive work return result})
export interface GlobalShim { moduleName: string /** `undefined` means side effect only */ globalName?: string /** * 1. `undefined` or empty string means `default`, for example: * * ```js * import globalName from 'module-name' * ``` * * 2. `null` means namespaced, for example: * * ```js * import * as globalName from 'module-name' * ``` */ named?: string | null /** * If not `false`, the shim will only be applied when the original * `globalName` unavailable, for example you may only want polyfill * `globalThis.fetch` when it's unavailable natively: * * ```js * import { fetch } from 'node-fetch-native'
Options
execArgv same as env SYNCKIT_EXEC_ARGV
globalShims: Similar like env SYNCKIT_GLOBAL_SHIMS but much more flexible which can be a GlobalShimArray, see GlobalShim's definition for more details
SYNCKIT_EXEC_ARGV: List of node CLI options passed to the worker, split with comma ,. (default as []), see also node docs
SYNCKIT_GLOBAL_SHIMS: Whether to enable the default DEFAULT_GLOBAL_SHIMS_PRESET as globalShims
SYNCKIT_TIMEOUT: timeout for performing the async job (no default)
SYNCKIT_TS_RUNNER: Which TypeScript runner to be used, it could be very useful for development, could be 'node' | 'ts-node' | 'esbuild-register' | 'esbuild-runner' | 'oxc' | 'swc' | 'tsx', node or ts-node will be used by default accordingly, make sure you have installed them already
TypeScript
node (Default, Node 22.6+)
On recent Node versions, you may select this runner to execute your worker file (a .ts file) in the native runtime.
As of Node v23.6, this feature is supported out of the box. For Node>=22.6 <23.6, this feature is supported via --experimental-strip-types flag. Visit the documentation to learn more.
When synckit detects the process to be running with supported Node versions (>=22.6), it will execute the worker file with the node runner by default, you can disable this behavior by setting --no-experimental-strip-types flag via NODE_OPTIONS env or cli arg.