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.
Create an HTML file to link your JavaScript files.
Use the <script>
tag to include each JS file.
<!-- 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.
Modify the <script>
tags to defer the loading of the JavaScript files.
Add the defer
attribute to each <script>
tag.
<!-- 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.
Ensure your JS files are served from a server or have the correct module type when included in HTML.
Use the import
statement to include one JS file into another.
// 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
.
Use the *
syntax to import all exports from a module into a single object.
// 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.
Employ dynamic imports in situations where you need to load a module conditionally or on-demand.
Use the import()
function inside an asynchronous function to load your module.
// 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.
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.