How can I export all functions from a file in JS?

JavascriptEcmascript 6

Javascript Problem Overview


I'm creating a unit converter, and I want to put all of the conversion functions into their own file. Using ES6 export, is there any way to export all of the functions in the file with their default names using only one line? For example:

export default all;

The functions are all just in the file, not within an object.

Javascript Solutions


Solution 1 - Javascript

No, there's no wildcard export (except when you're re-exporting everything from another module, but that's not what you're asking about).

Simply put export in front of each function declaration you want exported, e.g.

export function foo() {
    // ...
}
export function bar() {
    // ...
}

...or of course, if you're using function expressions:

export var foo = function() {
    // ...
};
export let bar = () => {
    // ...
};
export const baz = value => {
    // ...
};

Solution 2 - Javascript

I think there are a lot of solutions to this. And as has been answered, there's no wildcard export. But, you can 'wildcard' the import. So, I much prefer the one putting export before each of the functions you want to expose from the file:

//myfile.js
export function fn1() {...} 
export function fn2() {...}

and then import it like so:

import * as MyFn from './myfile.js'

Afterwards you could use it like so:

MyFn.fn1();
MyFn.fn2();

Solution 3 - Javascript

You can also use module.exports as follows:

function myFunction(arg) {
  console.debug(arg);
}
function otherFunction(arg) {
  console.error(arg);
}

module.exports = {
  myFunction: myFunction,
  otherFunction: otherFunction,
};

Then you can import it:

import {myFunction, otherFunction} from "./Functions.js";

Solution 4 - Javascript

In my use case, I do have three reusable functions in one file.

utils/reusables.js

export const a = () => {}
export const b = () => {}
export const c = () => {}

In order to point the root folder instead of individual file names, I created a file called index.js which will comprise of all the functions that are listed in individual files.

utils/index.js

export * from './reusables'

Now, when I want to use my a function, I will have to simply import it like this

import { a } from '../utils'

Rather than calling it from its individual files

import { a } from '../utils/reusables'

Solution 5 - Javascript

You could also export them at the bottom of your script.

function cube(x) {
  return x * x * x;
}

const foo = Math.PI + Math.SQRT2;

var graph = {
  options: {
      color:'white',
      thickness:'2px'
  },
  draw: function() {
      console.log('From graph draw function');
  }
}

export { cube, foo, graph };

You can also aggregate submodules together in a parent module so that they are available to import from that module.

// In parentModule.js
export { myFunction, myVariable } from 'childModule1.js';
export { myClass } from 'childModule2.js';

// In top-level module
import { myFunction, myVariable, myClass } from 'parentModule.js'

Solution 6 - Javascript

For Node.js environment, what I did to export functions was this.

UserController.js

module.exports = {
  signUp: () => {
    return "user"
  },
  login: () => {
    return "login"
  }
}

UserRouter.js

const UserController = require('./UserController')

then login and signUp functions could be used inside UserRouter as UserController.signUp() and UserController.login()

Solution 7 - Javascript

I think there's a missing common solution, which is exporting in index.js file:

myModule/myFunctions.js

export const foo = () => { ... }
export const bar = () => { ... }

then in myModule/index.js

export * from "./myFunctions.js";

This way you can simply import and use it with:

import { foo, bar } from "myModule";
foo();
bar();

Solution 8 - Javascript

functions.js

function alpha(msj) {
    console.log('In alpha: ' + msj);
}

function beta(msj) {
    console.log('In beta: ' + msj);
}

module.exports = {
    alpha,
    beta
};

main.js

const functions = require('./functions');
functions.alpha('Hi');
functions.beta('Hello');

Run

node main.js

Output

In alpha: Hi
In beta: Hello

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakAttk123View Question on Stackoverflow
Solution 1 - JavascriptT.J. CrowderView Answer on Stackoverflow
Solution 2 - JavascriptionizerView Answer on Stackoverflow
Solution 3 - JavascriptscotView Answer on Stackoverflow
Solution 4 - JavascriptcoderpcView Answer on Stackoverflow
Solution 5 - JavascriptCloudBranchView Answer on Stackoverflow
Solution 6 - JavascriptPavinduView Answer on Stackoverflow
Solution 7 - JavascriptEmzawView Answer on Stackoverflow
Solution 8 - JavascriptalditisView Answer on Stackoverflow