Skip to content

Remove extension check on Workers since they can run with require extensions #39124

@CyriacBr

Description

@CyriacBr

Is your feature request related to a problem? Please describe.
Currently workers can only run ".js", ".mjs", or ".cjs" files. However, you can pass them, through execArgv, a preloaded module to handle any other extension. This is the case with babel-register or esbuild-register, allowing node to run ts files on the fly. node -r esbuild-register foo.ts.

This would work if it weren't for the extension check:

const { Worker } = require("worker_threads");

const worker = new Worker("./test.ts", {
  execArgv: ["--require", "esbuild-register"],
});

Describe the solution you'd like
Maybe the current extension check is not needed, or should be able to be manually disabled through an option.

Describe alternatives you've considered
A workaround is to monkeypatch path.extname to masquerade your worker file as .js file. While it is obviously inappropriate, it shows that everything indeed works without any extension check.

const path = require("path");
const oldExtname = path.extname;
path.extname = filename => filename.endsWith('test.ts') ? ".js" : oldExtname(filename);

const { Worker } = require("worker_threads");

const worker = new Worker("./test.ts", {
  execArgv: ["--require", "esbuild-register"],
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.workerIssues and PRs related to Worker support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions