Import, Export, and Require
Very small programs, such as the kind often found in books on programming, can work just fine with all the code in a single file. Most of the time, applications will be made up of multiple files, often referred to as modules. Some modules may be dependencies installed from Node Package Manager (npm
) and some may be modules you or your team have written. When you look at other projects, you may see the keywords import
, export
, module
, and require
used to link different modules together. import
and require
both serve the same purpose. They allow you to use another module in the module (file) you are currently working in. export
and module
are the opposite. They allow you to make part or all of your module available for other modules to use.
We'll go over the different syntax options here. The reason for multiple ways to do things has, as usual, to do with the way the languages and runtimes have evolved. Node.js is by far the most popular runtime for...