Import all Exports from a File in JavaScript or TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 29, 2024
4 min

banner

# Import all Exports from a File in JavaScript or TypeScript

To import all exports from a file in JavaScript or TypeScript:

  1. Export all members you intend to import from file A.
  2. Import all the exports in file B as import * as myObj from './another-file'.
  3. Use the imported members as myObj.myFunction() in file B.

Here is an example of exporting multiple values from a file called another-file.js.

another-file.js
// ๐Ÿ‘‡๏ธ all named exports export const getEmployee = () => { return {id: 3, salary: 300}; }; export function getDeveloper() { return {name: 'Bobby Hadz'}; } export const department = 'front-end';

Here is how we would import all of the exports into a file called index.js.

index.js
import * as company from './another-file.js'; const emp = company.getEmployee(); console.log(emp); // ๐Ÿ‘‰๏ธ {id: 3, salary: 300} const dev = company.getDeveloper(); console.log(dev); // ๐Ÿ‘‰๏ธ {name: 'Bobby Hadz'} console.log(company.department); // ๐Ÿ‘‰๏ธ "front-end"

import all exports from file

The code for this article is available on GitHub

If you use TypeScript, you would have to omit the extension when importing.

index.ts
// Importing in TypeScript import * as company from './another-file'; const emp = company.getEmployee(); console.log(emp); // ๐Ÿ‘‰๏ธ {id: 3, salary: 300} const dev = company.getDeveloper(); console.log(dev); // ๐Ÿ‘‰๏ธ {name: 'Bobby Hadz'} console.log(company.department); // ๐Ÿ‘‰๏ธ "front-end"

This pattern is called creating a module object and is used when importing a large number of things.

The syntax grabs all of the exports from another-file.js and makes them available as members of an object called company inside of the index.js file.

# Specify the correct path when importing

Make sure to correct the path that points to the another-file.js module if you have to. The example above assumes that another-file.js and index.js are located in the same directory.

For example, if another-file.js was located one directory up, you'd have to import as import * as company from '../another-file.js'.

The import/export syntax is called JavaScript modules.

In order to be able to import something from a different file, it has to be exported using a named or default export.

The example above uses named exports.

# Explicitly importing all exports from another file

You could have also used named imports in index.js and explicitly stated the names of the imports.

index.js
// ๐Ÿ‘‡๏ธ named imports import {getEmployee, getDeveloper, department} from './another-file.js'; const emp = getEmployee(); console.log(emp); // ๐Ÿ‘‰๏ธ {id: 3, salary: 300} const dev = getDeveloper(); console.log(dev); // ๐Ÿ‘‰๏ธ {name: 'Bobby Hadz'} console.log(department); // ๐Ÿ‘‰๏ธ "front-end"

explicitly importing all exports from another file

The code for this article is available on GitHub

If you use TypeScript, you have to omit the extension when importing.

index.ts
// Example in TypeScript (omit the extension) // ๐Ÿ‘‡๏ธ named imports import {getEmployee, getDeveloper, department} from './another-file';

The JS code sample achieves the same goal as importing all exports from the file via import * as company from './another-file.js'.

This approach is more widely used as it is more concise and direct.

The module you are importing from might also contain a default export.

The main difference between named and default exports and imports is that you can have multiple named exports per file, but you can only have a single default export.

# Using the module object pattern when importing

If the module you are importing from has a default export, you would import it by removing the curly braces around its name or accessing it on the namespace as myObj.default.

another-file.js
// ๐Ÿ‘‡๏ธ default export export default function sum(a, b) { return a + b; }

And here is how you would import and use the default export with the module object pattern.

index.js
import * as myObj from './another-file.js'; console.log(myObj.default(50, 50)); // ๐Ÿ‘‰๏ธ 100

using the module object pattern when importing

The code for this article is available on GitHub

Notice that the default export is a function named sum but we access it as .default with this pattern.

A more intuitive approach is to import the things you need directly.

another-file.js
// ๐Ÿ‘‡๏ธ all named exports export const getEmployee = () => { return {id: 3, salary: 300}; }; export function getDeveloper() { return {name: 'Bobby Hadz'}; } export const department = 'front-end'; // ๐Ÿ‘‡๏ธ default export export default function sum(a, b) { return a + b; }

And here is how you would import the members you need.

index.js
import sum, {getEmployee, getDeveloper, department} from './another-file.js'; console.log(sum(50, 50)); // ๐Ÿ‘‰๏ธ 100 console.log(getEmployee()); // ๐Ÿ‘‰๏ธ {id: 3, salary: 300} console.log(getDeveloper()); // ๐Ÿ‘‰๏ธ {name: 'Bobby Hadz'} console.log(department); // ๐Ÿ‘‰๏ธ 'front-end

In my experience, most real-world codebases exclusively use named exports and imports because they make it easier to leverage your IDE for auto-completion and auto-imports.

You also don't have to think about which members are exported using default or named export.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev