63

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 function 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();
4
  • Are you getting an error? Commented Dec 23, 2015 at 22:53
  • No I'm not getting an error. But I'm also not exporting anything from mod. Notice there are no export statements. Commented Dec 23, 2015 at 22:54
  • Can you elaborate on how you are expecting to use this elsewhere? I'm not clear from your question what you're trying to achieve.. Commented Dec 23, 2015 at 22:59
  • I've updated the question to provide an example. Commented Dec 23, 2015 at 23:03

2 Answers 2

127

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The only way that seems to work is import mod from "./inner/mod"; export {mod}. Using export * from './inner/mod'; doesn't seem to work.
Actually, it does work. It doesn't work, however, if inner/mod exports a default function.
Yep, that will only cover named exports. You can explicitly export the default export with export {default} from './inner/mod';.
with firebase cloud functions, in typescript : File A (sub file) export const api = functions.https.onRequest(app); File B (index) export * from './api';
38
// 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.

5 Comments

Is it possible to export the entire object? I want to have an index module that exports all of the sub-modules.
Ow yes it is, you can export anything static, objects, arrays, strings, numbers etc. What happens is that everything exported goed into a key-value object. The key being the name of the export, the value the .. well the thing you're exporting of course.. You can't export expressions.
I'm using babel to convert es6 to es5. Using your example I get an unexpected token on export myFunc.
Updated the code, does this work? I've only been using this syntax myself for a few days, 2ality.com/2014/09/es6-modules-final.html is an amazing resource.
export {myFunc as myFunc}; can be written as export {myFunc};.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.