Fireship · @fireship

JavaScript Modules in 100 Seconds

Uploaded January 2, 2020 · Length 1:44

JavaScript Modules in 100 Seconds

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.

  1. 0:00

    let's say you have some JavaScript code

  2. 0:01

    that lives in a single file you can turn

  3. 0:03

    this code into a module by exporting it

  4. 0:05

    this allows other files to use an import

  5. 0:07

    statement to import this file as a

  6. 0:09

    dependency so a module is nothing more

  7. 0:12

    than a file that exports its own code

  8. 0:13

    this allows developers to organize code

  9. 0:15

    within their own projects or share code

  10. 0:17

    with the world through package managers

  11. 0:19

    like NPM in modern front-end development

  12. 0:21

    it's most common to use es modules which

  13. 0:23

    became an official language feature in

  14. 0:25

    2015 the syntax is also supported in

  15. 0:27

    note 13 but traditionally the way to do

  16. 0:29

    this a node is with required Jas it's a

  17. 0:32

    similar idea but instead we use this

  18. 0:34

    require function to import a module

  19. 0:36

    require is more common now but the

  20. 0:38

    future is es modules a module can export

  21. 0:41

    its code in a variety of ways and this

  22. 0:43

    will affect the way the consumer imports

  23. 0:45

    it a module can export a single default

  24. 0:47

    value multiple values or both if a

  25. 0:50

    module provides a default export it

  26. 0:51

    means the consumer can call import name

  27. 0:54

    from path the importing file can choose

  28. 0:56

    whatever name it wants but a module also

  29. 0:58

    has the option to expose multiple

  30. 1:00

    exports from a single file you can

  31. 1:02

    export them one by one or all together

  32. 1:04

    as a single export list on the other

  33. 1:07

    side the importer will use the exact

  34. 1:09

    name used in the module however it is

  35. 1:11

    possible to rename an import using the

  36. 1:13

    as keyword a huge benefit of named

  37. 1:15

    exports is that they make the module

  38. 1:17

    tree shakable that means the consumer

  39. 1:19

    only imports the code they actually need

  40. 1:21

    resulting in smaller bundle sizes and

  41. 1:23

    better performance you can also combine

  42. 1:25

    the best of both worlds to give you a

  43. 1:26

    hybrid syntax that looks like this it

  44. 1:28

    helps to have a cheat sheet my personal

  45. 1:30

    favorite is the one from Samantha Ming

  46. 1:32

    if you don't already follow her awesome

  47. 1:34

    post on Twitter you definitely should be

  48. 1:35

    this has been yes modules and 100

  49. 1:37

    seconds thanks for watching and I will

  50. 1:39

    see you in the next one