ES Modules
Learn how to import functions and variables from other files and libraries
ES Modules are very useful because they let you encapsulate all sorts of functionality, and expose this functionality to other JavaScript files, as libraries.
In this unit weâll find out how to create our own modules and how to import values from other files.
Using import and export
A module is a JavaScript file that exports one or more values (it can be objects, functions or variables), using the export
keyword.
For example, a file can export a variable:
Or a function:
test.js
You can then import this value in another file.
Suppose the file is named test.js
and itâs found in the same folder as the one you want to import from, youâll write
And you can use that function as if it was written in the same file.
Notice I used ./
to say âcurrent fileâs folderâ. If the file is in another folder, youâll need a relative path, for example ../../test.js
if itâs 2 subfolders deep.
Using server-side JavaScript, when weâll get to that (Node / Deno / Bun), youâll also see how to import from built-in modules and 3rd party modules.
ES Modules: .mjs files
What we wrote in the first ES modules lesson is correct, but it needs to run in an environment where ES modules are enabled.
Most modern tools like Next.js, Astro, SvelteKit etc will sort this out for you.
But if you try creating a test.js
file in a folder with an export and you try to run it with node test.js
(Node.js, not yet covered but bear with me):
test.js
Youâll get an error like this:
It doesnât work!
What we need to do is listen to what that error message says.
You can rename both files from .js
to .mjs
, making sure you also update the import path from './test.js'
to './test.mjs'
, and things will work.
Or you can add a package.json
file in the folder with this content:
and that will work without changing the file extension.
Default exports
Sometimes youâll see the default
keyword being used to create default exports.
A default export is usually made when you export one single thing from a file:
test.js
Then you can import it from another file using this syntax:
It seems simpler, but the problem (and the reason why this approach is generally avoided) is that we can import that value assigning any name we want, for example:
and it works in the same way, name
is assigned the value of the default export age
.
Also, we cannot have more than one default export, so if you want to export something else later on youâll have to use named exports.
Note that you can also use both at the same time, not something youâd do usually, but some libraries sometimes offer this option to offer a more granular access to what they export:
test.js
Multiple exports
Weâve seen how to export a value from a JavaScript file:
We can export more by simply adding more values to the export:
test.js
A file that wants to use them will be able to use both, or just pick the values it needs:
Or, you can import all:
In this case youâll have all the variables exported by the exporting file available in the importing file.
Renaming exports
You can rename any import, for convenience, using as
:
A file can export the value a
test.js
but you want to use it in another file with a more descriptive name, for example:
Using the `script` tag
An HTML page can load a module by using a <script>
tag with the special type="module"
attribute:
So now inside your page you can use the import syntax to get access to the values exported by that module.
Note: this module import behaves like a
defer
script load. See efficiently load JavaScript with defer and async
Itâs important to note that any script loaded with type="module"
is loaded in strict mode.
Last updated
Was this helpful?