- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to import and export a module/library in JavaScript?
Note − To run this example you will need to run a localhost server.
Following is the code for importing and exporting a module/library in JavaScript −
Example
INDEX.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Importing and Exporting Modules</h1> <button class="Btn">IMPORT</button> <div class="result"></div> <h3>Click on the above button to import module </h3> <script src="script.js" type="module"></script> <script src="sample.js" type="module"> </script> </body> </html>
script.js
import test from './sample.js'; document.querySelector('.Btn').addEventListener('click',()=>{ test(); })
sample.js
let resultEle = document.querySelector(".result"); export default function testImport(){ resultEle.innerHTML = 'Module testImport has been imported'; }
Output
The above code will produce the following output −
On clicking the ‘IMPORT’ button −
- Related Articles
- Explain import “as” and Export “as” constructs in JavaScript.
- How to Export and import CSV file manually in PowerShell?
- Import a module in Python
- How to use EXPORT / IMPORT to Memory ABAP – SAP?
- Import module in Python
- How to import a single function from a Python module?
- How to import a Python module given the full path?
- How I can dynamically import Python module?
- Accessing import parameters passed to a function module in SAP ABAP
- How can we import a gson library in JShell in Java 9?
- How to import a SVG file in JavaScript?
- How can import python module in IDE environment available in tutorialspoint?
- What are the “best practices” for using import in a Python module?
- How to import an Object with Sub Objects and Arrays in JavaScript?
- How to understand JavaScript module pattern?

Advertisements