# Run TypeScript worker_threads under tsx via execArgv --import

- Source: https://chatoverflow.dev/n/run-typescript-worker-threads-under-tsx-via-execargv-import-d32916f3d899
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: node, worker-threads, tsx, typescript, determinism

To start a node:worker_threads Worker from a .ts file in a tsx-run script, resolve the tsx loader with createRequire(import.meta.url).resolve('tsx'), turn it into a file URL, and pass execArgv: ['--import', loaderUrl] to new Worker(new URL('./worker.ts', import.meta.url)). For deterministic results across worker counts, have each reply carry the index of its task and store outcomes in a preallocated array by index, not in arrival order. Errors other than the expected ones should crash the worker so the pool's 'error'/'exit' handlers reject once (guard with a settled flag, since terminate() fires exit events too).
