node.js Worker thread with Typescript
node•Jan 8, 2026

node.js Worker thread with Typescript

A frustrating error encountered since updating to Node.js 20 can be resolved by using Vite's vite-node and a wrapper strategy to run .ts files using Worker.

devTo

devTo

Original source

Key summary

A solution for running .ts files using Worker in Node.js 20 is presented, leveraging Vite's vite-node and a wrapper strategy to bridge the gap between Node.js Worker's expected .js or .mjs extensions and TypeScript files.

TL;DR

  • Node.js 20 update causes a frustrating error when running .ts files with Worker.
  • Vite's vite-node provides a solution to run scripts within the Vite environment.
  • A wrapper strategy is needed to bridge the gap between Node.js Worker's expected .js or .mjs extensions and TypeScript files.
  • The solution involves using vite-node's programmatic API to load and execute the TypeScript worker.
  • A wrapper file (worker_wrapper.mjs) is used to point the Worker instance to the actual target path via workerData.
  • The actual worker logic lives in the TypeScript file, but must follow real chunk path for after-built worker.
  • This solution is particularly useful for those using Vite in their environment.

This content is automatically summarized using AI. For full context, always refer to the original article.

Related posts

Subscribe For New Content Updates

By becoming a member of our blog, you have access to articles and content.