Max
Max

Reputation: 15955

Export an imported module

I have two javascript modules that looks like this:

// inner/mod.js
export function myFunc() {
   // ...
}

// mod.js
import * as inner from "./inner/mod";

I would like to export myFunc from mod.js. How can I do this?

EDIT: I should clarify that the function is being exported as expected from inner/mod.js but I also want to export the funtion from the outer mod.js.

To those asking for clarification, I would like to achieve this:

// SomeOtherFile.js
import * as mod from "mod"; // NOT inner/mod

mod.myFunc();

Upvotes: 62

Views: 39979

Answers (2)

Felix Kling
Felix Kling

Reputation: 816262

I believe what you are looking for is

export * from './inner/mod';

That will reexports all exports of ./inner/mod. The spec actually has very nice tables listing all the possible import and export variants.

Upvotes: 123

Norbert
Norbert

Reputation: 663

// inner/mod.js
export function myFunc() {
   // ...
}

// mod.js
import { myFunc } from "./inner/mod";
export { myFunc };

Try to be explicit in what you import, the less the better, because of that I've changed your import in mod.js. If you do import *, you define a variable which will be the object of all names exports from that module you imported.

re-exporting is the same as making something of your own and exporting.

Upvotes: 37

Related Questions