JAN
JAN

Reputation: 21855

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 ?

Upvotes: 1

Views: 1094

Answers (1)

Andrei
Andrei

Reputation: 4617

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

Upvotes: 2

Related Questions