Bird
0
0

Which of the following is the correct way to import the Worker class from the worker_threads module?

easy📝 Syntax Q12 of 15
Node.js - Worker Threads
Which of the following is the correct way to import the Worker class from the worker_threads module?
Aconst Worker = require('worker_threads');
Bimport Worker from 'worker_threads';
Cconst { Worker } = require('worker_threads');
Dconst Worker = require('worker_threads').worker;
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct import syntax

    The Worker class is a named export, so we use destructuring: const { Worker } = require('worker_threads');
  2. Step 2: Check other options

    const Worker = require('worker_threads'); assigns the entire module object, not the Worker class. import Worker from 'worker_threads'; uses ES module syntax which requires extra config. const Worker = require('worker_threads').worker; uses wrong property name.
  3. Final Answer:

    const { Worker } = require('worker_threads'); -> Option C
  4. Quick Check:

    Destructure Worker from module = D [OK]
Quick Trick: Use destructuring to import Worker from worker_threads [OK]
Common Mistakes:
  • Using wrong property name like 'worker' instead of 'Worker'
  • Mixing CommonJS and ES module syntax incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes