CommonJS modules
In recent years, Node has been gaining popularity in the software industry; indeed it is becoming a very popular choice for backend development in a full JavaScript technology stack. If you don't know about Node, you can think about it as JavaScript used in the server instead of a browser.
Node uses the CommonJS module syntax for its modules; a CommonJS module is a file that exports a single value to be used for other modules. It is useful to use CommonJS because it provides a clean way to manage JavaScript modules and dependencies.
To support CommonJS, Node uses the require()
function. With require()
you can load JavaScript files without the need to use <script>
tags, instead calling require()
with the name of the module/dependency that you need and assigning it to a variable.
To illustrate how CommonJS modules work, let's write a Node module and see how to use the require()
function. The following code shows a simple module that exposes a simple object with the method...