Import one JS file into another in the plain JS

An easy tutorial on how to import one JS file into another using ES6

by Luigi Nori Date: 03-12-2021 javascript es6 es2015 include import export


Until some years ago, it was not possible to import one js file or block of code from one file into another but since 2015, ES6 has introduced useful ES6 modules. With the module standard, we can use import and export keyword and syntax given to include and export the module in JS respectively.

The static  import  statement is used to import bindings which are exported by another module. Imported modules are in strict mode whether you declare them as such or not. The import statement cannot be used in embedded scripts unless such script has a type="module".

There is also a function-like dynamic  import() , which does not require scripts of type="module".

Backward compatibility can be ensured using attribute nomodule on the script tag.

How we include the js files:

index.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="module" src="script2.js"></script>
</body>
</html> 

script1.js

export default function Hello() {
return "Hello buddy!";
}

script2.js

import { Hello } from './script1.js';
let val = Hello;
console.log(val);

Run the above sample code blocks in your local system/local server, you will get the console output as: Hello buddy!

Code and Syntax Explanation of the code above:

In script1.js, we exported the module with the syntax of : export module

Similarly, in our case, we exported the function Hello by writing the export keyword in the function definition (line number 1 in script1.js). In script2.js, we imported that module by writing the following code/syntax :

import { Hello } from './script1.js';

Right now in script1.js, we have only one module to export but what if we had multiple modules to export? That is also possible with a similar syntax but with a slight modification:

Sample Code for multiple modules:

script1.js

export let Name = (name) => {return "My name is " + name;}
export let Address = (address) => {return "i live in " + address;}
export let Age = (age) => {return "my age is "+ age;}

script2.js

import { Name, Address, Age} from './script1.js';
let val = Name("John");
let age = Age(26);
console.log(val + " and " + age);

In script1.js, we have three modules named Name, Address, and Age. In Script2.js, we imported those modules with similar but little different syntax.

import { Name, Address, Age} from './script1.js';

Note: we didn’t have { } in earlier import code because that was default export ( remember one file can have only one default export).

When we remove that default keyword in previous code of script1.js, we get an error in the console and that error looks like this:

Infact there are two types of exports:

  1. Named Exports (Zero or more exports per module)
  2. Default Exports (One per module)

This is only a quick explanation, you can fond more examples at MDN web docs:
Import Statement Reference at MDN
Export Statement Reference at MDN

 
by Luigi Nori Date: 03-12-2021 javascript es6 es2015 include import export hits : 65238  
 
Luigi Nori

Luigi Nori

He has been working on the Internet since 1994 (practically a mummy), specializing in Web technologies makes his customers happy by juggling large scale and high availability applications, php and js frameworks, web design, data exchange, security, e-commerce, database and server administration, ethical hacking. He happily lives with @salvietta150x40, in his (little) free time he tries to tame a little wild dwarf with a passion for stars.

 
 
 

Related Posts

How to use the endsWith method in JavaScript

In this short tutorial, we are going to see what the endsWith method, introduced in JavaScript ES6, is and how it is used with strings in JavaScript. The endsWith method is…

What are javascript symbols and how can they help you?

Symbols are a new primitive value introduced by ES6. Their purpose is to provide us unique identifiers. In this article, we tell you how they work, in which way they…

Callbacks in JavaScript

Callback functions are the same old JavaScript functions. They have no special syntax, as they are simply functions that are passed as an argument to another function. The function that receives…

How to create PDF with JavaScript and jsPDF

Creating dynamic PDF files directly in the browser is possible thanks to the jsPDF JavaScript library. In the last part of this article we have prepared a practical tutorial where I…

How to make your own custom cursor for your website

When I started browsing different and original websites to learn from them, one of the first things that caught my attention was that some of them had their own cursors,…

Node.js and npm: introductory tutorial

In this tutorial we will see how to install and use both Node.js and the npm package manager. In addition, we will also create a small sample application. If you…

How to connect to MySQL with Node.js

Let's see how you can connect to a MySQL database using Node.js, the popular JavaScript runtime environment. Before we start, it is important to note that you must have Node.js installed…

JavaScript Programming Styles: Best Practices

When programming with JavaScript there are certain conventions that you should apply, especially when working in a team environment. In fact, it is common to have meetings to discuss standards…

Difference between arrow and normal functions in JavaScript

In this tutorial we are going to see how arrow functions differ from normal JavaScript functions. We will also see when you should use one and when you should use…

JavaScript Arrow functions: What they are and how to use them

In this article we are going to see what they are and how to use JavaScript Arrow Functions, a new feature introduced with the ES6 standard (ECMAScript 6). What are Arrow…

How to insert an element into an array with JavaScript

In this brief tutorial you will learn how to insert one or more elements into an array with JavaScript. For this we will use the splice function. The splice function will not…

What is the difference between primitives types and objects in JavaScript?

In this short tutorial we are going to look at the differences between primitive types and objects in JavaScript. To start with, we're going to look at what primitive types…