Chapter 16 of 16All chapters
Chapter 16 of 16
Modules
Splitting code across files.
import and export
A file becomes a module when it exports something. Named exports come out by name, and a default export comes out under whatever name the importer picks.
- In a browser, a module needs type="module" on the script tag.
- Modules are deferred by default and run in strict mode.
Older formats
Node used require and module.exports for years, and plenty of code still does. The two systems can be mixed with care, but a new project should pick ES modules and stay there.