- CommonJS modules
- ES Modules
- The NodeJS standard library
JavaScript runtimes like NodeJS usually treat each JavaScript file as a separate module.
Think of a module as a box. The box has its own private space, so the variables and functions you declared in it don’t interfere with the code in other boxes. Basically, each module has its own scope.
A module can export some of its content, making it available to other modules, and it can import the content that other modules have exported. JavaScript allows you to export and import content between modules, to connect different files.
A JavaScript program is often composed of multiple modules, that are connected with each other.
Why use modules? By splitting your code into modules, you can organize your program into smaller, clearer, and reusable parts. Each module can focus on just one type of task, like handling math calculations, working with files, or formatting text.
CommonJS modules
In NodeJS, the most common system to manage modules is called CommonJS.
In this system, you can share (export) code from a module using
module.exports and load (import) it in another file using require().To make something available outside a module, you assign it to
module.exports:const greeting = "Hello!" module.exports = greeting
Here, the string
"Hello!" is what this module exports.To use the exported code from another file, you use the
require() function with the path to that file:const greeting = require("./greeting.js") console.log(greeting)
This prints:
Hello!
You can export multiple things by bundling them together in an anonymous object, like
const greeting1 = "hello" const greeting2 = "hi" module.exports = { greeting1, greeting2 }
CommonJS was the module system that was initially adopted by NodeJS. Later on ES modules were also added.
ES Modules
NodeJS also supports another type of module called ES Modules, which are popular in web development. They use the keywords
export and import.ES modules were developed for the browser and only later added to Node. If you want to use them, you might have to use
.mjs as a file extension instead of .js, depending on which Node version you're using.In one file callled
greeting.mjs we writeexport const greeting = "Hello!"
As you can see, we're exporting the constant directly where it gets defined
In another file called
index.mjs, we import it:import { greeting } from "./greeting.mjs" console.log(greeting)
You can export different declarations in different parts of the file, like
export const num = 10 export function double (x) { return x*2 }
The NodeJS standard library
NodeJS also includes many built-in modules. These are ready-made modules provided by NodeJS that help with common tasks like reading files, working with the operating system, or connecting to the network.
For example, the
os module gives you information about your operating system:const os = require("os") console.log(os.platform())
You don’t have to install these built-in modules, they come with NodeJS. They form the "standard library" of the runtime, and are used by most Node applications to do stuff like reading files or communicating via the internet.
The next chapters will show you some useful examples of their usage.