JavaScript Program to Include a JS file in Another JS file

Updated on December 19, 2024
Include a js file in another js file header image

Introduction

Including a JavaScript (JS) file in another JS file is a common practice in web development. This technique allows developers to modularize their code, making it easier to manage, maintain, and reuse across multiple projects. By splitting code into separate files based on functionality, you can keep your codebase clean and organized.

In this article, you will learn how to include JS files into another JS file using various methods. Explore practical examples that demonstrate the usage of script tags, module imports, and asynchronous module loading. This knowledge enhances code modularity and project maintainability.

Including JS Files Using Script Tags

Basic Inclusion with HTML

  1. Create an HTML file to link your JavaScript files.

  2. Use the <script> tag to include each JS file.

    html
    <!-- index.html -->
    <script src="lib.js"></script>
    <script src="app.js"></script>
    

    This HTML file links two JavaScript files. First, lib.js is loaded, followed by app.js. Ensure that any functions or variables in app.js that depend on lib.js are declared after it loads.

Deferring JavaScript Loading

  1. Modify the <script> tags to defer the loading of the JavaScript files.

  2. Add the defer attribute to each <script> tag.

    html
    <!-- index.html -->
    <script src="lib.js" defer></script>
    <script src="app.js" defer></script>
    

    By using defer, both scripts load after the HTML document is fully parsed but execute in the order they appear. This method is useful when scripts depend on the complete parsing of the HTML document.

Managing Dependencies with ES Modules

Importing Modules

  1. Ensure your JS files are served from a server or have the correct module type when included in HTML.

  2. Use the import statement to include one JS file into another.

    javascript
    // utilities.js
    export function add(x, y) {
      return x + y;
    }
    
    // app.js
    import { add } from './utilities.js';
    
    console.log(add(5, 3));
    

    Here, utilities.js exports a function named add, and app.js imports it. When using modules, make sure the <script> tag for the entry point (app.js) has the type attribute set to module.

Importing All Exports from a Module

  1. Use the * syntax to import all exports from a module into a single object.

    javascript
    // utilities.js
    export function add(x, y) {
      return x + y;
    }
    export function subtract(x, y) {
      return x - y;
    }
    
    // app.js
    import * as Utilities from './utilities.js';
    
    console.log(Utilities.add(10, 5));
    console.log(Utilities.subtract(10, 5));
    

    This method bundles all the exports from utilities.js under the Utilities object, making them accessible via this single object.

Dynamically Loading Modules with Async/Await

Using async/await to Load Modules

  1. Employ dynamic imports in situations where you need to load a module conditionally or on-demand.

  2. Use the import() function inside an asynchronous function to load your module.

    javascript
    // app.js
    async function loadUtilities() {
      const module = await import('./utilities.js');
      console.log(module.add(20, 22));
    }
    
    loadUtilities();
    

    import() returns a promise, making it perfect for use with async/await. This example dynamically loads utilities.js when loadUtilities() is called.

Conclusion

Including a JavaScript file in another through various methods like script tags, ES modules, and dynamic imports, enhances modularity, maintainability, and scalability of your projects. Employ these techniques based on your project requirements: use script tags for simple cases, ES modules for better structuring and bundling, and dynamic imports for loading code on demand. These methods ensure that your web applications remain efficient, organized, and easy to manage across different environments and development stages.