Bird
0
0

Identify the mistake in this worker thread initialization code:

medium📝 Debug Q6 of 15
Node.js - Worker Threads
Identify the mistake in this worker thread initialization code:
const { Worker } = require('worker_threads');
const worker = new Worker('worker.js');
worker.on('message', msg => console.log(msg));
AThe worker script path should be an absolute or relative path with './'
BThe Worker constructor requires a second argument specifying the number of threads
CThe 'message' event should be 'data' instead
DThe 'worker_threads' module cannot be used in Node.js
Step-by-Step Solution
Solution:
  1. Step 1: Check worker script path

    Worker constructor expects a file path; relative paths must start with './' or '../'.
  2. Step 2: Evaluate options

    The worker script path should be an absolute or relative path with './' correctly identifies missing './' in 'worker.js'.
  3. Final Answer:

    The worker script path should be an absolute or relative path with './' -> Option A
  4. Quick Check:

    Always prefix relative paths with './' or '../' for Worker [OK]
Quick Trick: Relative paths need './' prefix when creating Worker [OK]
Common Mistakes:
  • Omitting './' in relative worker script paths
  • Using incorrect event names like 'data' instead of 'message'
  • Believing worker_threads is unsupported in Node.js

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes