'How can I import all the function from other file to current file?

I have one file containing many functions. Similar to below,

export const func1 = () => {
}

export const func2 = () => {
}

export const func3 = () => {
}

export const func4 = () => {
}

Now I want to use all this function in another file.

Method 1 is import {func1, func2, func3, func4} from 'path'.
Method 2 (which I don't want to use) is import * as access from 'path'

Method 1 approach is what I am using right now. But when func file gets bigger, this will not be readable. So, how to use the functions without creating aliases and simply func1() and not access.func1() ?



Solution 1:[1]

You can import with your Method 2 and then can use object destructuring on the access object like this and use them directly.

import * as access from 'path';

const {func1, func2, func3, func4} = access;

func1();

Solution 2:[2]

You should really make up your mind between the two as they are the most readable and clean solutions.

First option can be made more readable with:

import {
  func1,
  func2,
  func3,
  func4
} from 'path';

Nevertheless, if you really want an alternative syntax without having to list the exports, it is technically possible to load all the exported properties into the global object through:

Object.assign(globalThis, require('path'))

However, I would highly advise against this as it'll make your code very difficult to maintain. Global variables are painful enough, but unknown global variables coming from another file will be a nightmare. Use at your own risk.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2