Bird
0
0

You wrote this code to create a worker thread but it throws an error:

medium📝 Debug Q6 of 15
Node.js - Worker Threads
You wrote this code to create a worker thread but it throws an error:
const { Worker } = require('worker_threads');
const worker = new Worker('console.log("Hello")');
What is the problem?
AYou must use cluster.fork() instead of Worker
BWorker constructor requires a file path or URL, not inline code string
CYou forgot to import the cluster module
DWorker threads do not support console.log
Step-by-Step Solution
Solution:
  1. Step 1: Check Worker constructor argument

    The Worker constructor expects a file path or URL, not a code string.
  2. Step 2: Identify correct usage

    To run inline code, you must use a data URL or separate file.
  3. Final Answer:

    Worker constructor requires a file path or URL, not inline code string -> Option B
  4. Quick Check:

    Worker needs file path or URL [OK]
Quick Trick: Worker constructor needs file path or URL [OK]
Common Mistakes:
  • Passing inline code string directly
  • Confusing cluster with worker_threads
  • Assuming console.log unsupported in workers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes