Lesson 21. Using modules

 

After reading lesson 21, you will

  • Understand how to specify the location of a module you intend to use
  • Understand all the various ways of importing values from modules
  • Understand how to import modules for side effects
  • Understand the order of execution of code when importing modules
  • Be able to break apart large modules into smaller ones

Modules are a great way to separate your logic into cohesive units and share logic across files without the cumbersome use of global variables. They also allow you to import only what is needed, keeping the cognitive load down and making maintenance easier. In the previous lesson you learned what a module is and the basics of how to create a module and export values. In this lesson we’ll look at using other modules, the various ways of importing values, and how to break apart and organize your code using modules.

Consider this

Imagine that you’re writing a web application and you need to use a few third-party open source libraries. The problem is that two of those libraries both expose themselves using the same global variable. How would you make the two libraries work together?

21.1. Specifying a module’s location

21.2. Importing values from modules

21.3. How imported values are bound

21.4. Importing side effects

21.5. Breaking apart and organizing modules

Summary