Hacktoberfest 2026: die Issues, die Maintainer für den Oktober markiert haben – offen und einsteigerfreundlich. Hacktoberfest-Issues durchsuchen

can't import web-worker inside a NodeJS worker thread with no workerData

Offen
#57 1 Kommentar 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
62/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Ruhig
Tech-Stack
javascript, node.js
Bereich
backend

Rechercherichtung

Führen Sie zuerst die bereitgestellte Reproduktion mit main.js und worker.js aus und untersuchen Sie anschließend src/node/index.js rund um workerThread in Zeile 141 sowie den zugehörigen Importpfad. Als erledigt gilt dies, wenn das Importieren von web-worker innerhalb eines unabhängigen NodeJS-worker-Threads mit undefined workerData keine Exception mehr auslöst, während das eigene worker-thread-Verhalten der Bibliothek intakt bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Simply having an import to web-worker inside a NodeJS worker thread with no workerData will result in the following error:

node:internal/event_target:1101
  process.nextTick(() => { throw err; });
                           ^
TypeError [Error]: Cannot destructure property 'mod' of 'threads.workerData' as it is undefined.
    at workerThread (file:///home/jonas/projects/temp/web-worker-in-worker-test/node_modules/web-worker/src/node/index.js:141:8)
    at file:///home/jonas/projects/temp/web-worker-in-worker-test/node_modules/web-worker/src/node/index.js:75:94
    at ModuleJob.run (node:internal/modules/esm/module_job:345:25)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:651:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5)

Node.js v22.19.0

Reproduction

main.js
import { Worker } from 'node:worker_threads';

const worker = new Worker('./worker.js', {});
setTimeout(() => worker.terminate(), 1000);
worker.js
import { parentPort } from 'node:worker_threads';
import Worker from 'web-worker';

parentPort.addEventListener('message', e => {
    // omitted for brevity, as it is not relevant
});

Notes

I saw that #40 tried to fix this issue already, but didn't fix it completely. The fix assumes workerData is set to a value. If it is undefined, we still run into the same issue as described in #40:

[...] When using this library within a worker thread, the workerThread() code is executed, rather than the mainThread() code which creates the worker polyfill.
[...]
This change executes workerThread() only if the mod property is present from the workerData, indicating that it's being executed from a thread created within this library. If mod isn't present, this means that the code is executing within the context of some other worker thread that wasn't created from this library, and it needs to treat the current thread as the main thread by executing mainThread().

Vorherrschende Sprache
JavaScript
Sterne
1.2k
Forks
57
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus developit/web-worker

Alle Issues in developit/web-worker

Ähnliche Issues

Weitere Issues zu JavaScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.