Fireship · @fireship
JavaScript Modules in 100 Seconds
Uploaded January 2, 2020 · Length 1:44
Summary
javascript modules explained in 100 seconds, including es modules and require function.
- javascript modules
- es modules
- require function
- javascript dependencies
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
let's say you have some JavaScript code
- 0:01
that lives in a single file you can turn
- 0:03
this code into a module by exporting it
- 0:05
this allows other files to use an import
- 0:07
statement to import this file as a
- 0:09
dependency so a module is nothing more
- 0:12
than a file that exports its own code
- 0:13
this allows developers to organize code
- 0:15
within their own projects or share code
- 0:17
with the world through package managers
- 0:19
like NPM in modern front-end development
- 0:21
it's most common to use es modules which
- 0:23
became an official language feature in
- 0:25
2015 the syntax is also supported in
- 0:27
note 13 but traditionally the way to do
- 0:29
this a node is with required Jas it's a
- 0:32
similar idea but instead we use this
- 0:34
require function to import a module
- 0:36
require is more common now but the
- 0:38
future is es modules a module can export
- 0:41
its code in a variety of ways and this
- 0:43
will affect the way the consumer imports
- 0:45
it a module can export a single default
- 0:47
value multiple values or both if a
- 0:50
module provides a default export it
- 0:51
means the consumer can call import name
- 0:54
from path the importing file can choose
- 0:56
whatever name it wants but a module also
- 0:58
has the option to expose multiple
- 1:00
exports from a single file you can
- 1:02
export them one by one or all together
- 1:04
as a single export list on the other
- 1:07
side the importer will use the exact
- 1:09
name used in the module however it is
- 1:11
possible to rename an import using the
- 1:13
as keyword a huge benefit of named
- 1:15
exports is that they make the module
- 1:17
tree shakable that means the consumer
- 1:19
only imports the code they actually need
- 1:21
resulting in smaller bundle sizes and
- 1:23
better performance you can also combine
- 1:25
the best of both worlds to give you a
- 1:26
hybrid syntax that looks like this it
- 1:28
helps to have a cheat sheet my personal
- 1:30
favorite is the one from Samantha Ming
- 1:32
if you don't already follow her awesome
- 1:34
post on Twitter you definitely should be
- 1:35
this has been yes modules and 100
- 1:37
seconds thanks for watching and I will
- 1:39
see you in the next one


