'How to require multiple exports from a module without naming all of them?

I'm requiring a lot of exports from a module as follows:

employee.js

// Some logic goes here
module.exports = {
    AvgEmployees,
    AvgDailyEmployee,
    AvgAbsenceEmployee,
    AvgWorkDaysEmpolyee,
    ...
    ..
    ..
};

main.js

const {
  AvgEmployees,
  AvgDailyEmployee,
  AvgAbsenceEmployee,
  AvgWorkDaysEmployee,
  ...
  ...
  ...
} = require('src/employee.js');

// Do what's needed with all the imports
// ....
// ...

I'm importing more than 50 imports and it starts to bother me every time to name all the imports.

Is it possible to import all of them at once without naming each one specifically ?



Solution 1:[1]

Yes, like this:

const employee = require('src/employee.js');

// Do what's needed with all the imports
employee.AvgEmployees
employee.AvgDailyEmployee
// ....
// ...

Variable employee is the value of module.exports and const { ... } = require('src/employee.js'); is just object destructuring: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1