Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(); 
like image 356
Max Avatar asked Dec 23 '15 22:12

Max


People also ask

Can you use module exports with import?

Modules can also contain a default export, using the default keyword. A default export will not be imported with curly brackets, but will be directly imported into a named identifier.

Can you export with module exports?

Exporting values with just the exports keyword is a quick way to export values from a module. You can use this keyword at the top or bottom, and all it does is populate the module. exports object. But if you're using exports in a file, stick to using it throughout that file.

What is export and import in JavaScript?

Import. You can import modules into a file in two ways, based on if they are named exports or default exports. Named exports are constructed using curly braces. Default exports are not.


1 Answers

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.

like image 126
Felix Kling Avatar answered Sep 28 '22 19:09

Felix Kling