Advertisement
Beginner JavaScript, ES Modules

How do you import everything from a module under one namespace object?

Short answer

Use import * as name from '...' to collect all of a module's named exports into a single object, accessed as name.exportedThing.

Save JavaScript mock interview
Advertisement
AModel answer
Use import * as name from '...' to collect all of a module's named exports into a single object, accessed as name.exportedThing.
import * as MathUtils from './math.js';
console.log(MathUtils.PI);
MathUtils.default(2, 3); // calling the default export
Real-world example
Importing an entire utility library under one namespace to avoid dozens of individual named imports.
Likely follow-ups
  • Does this also include the default export, and if so, under what key?
Related topics