September 2018
Intermediate to advanced
764 pages
20h 6m
English
In Node.js, modules are written in the CommonJS format, which provides two global objects, require and exports, that developers can use to keep their modules encapsulated. require is a function that allows the current module to import and use variables defined in other modules. exports is an object that allows a module to make certain variables publicly available to other modules that require it.
For example, we can define two functions, helloWorld and internal, in greeter.js:
// greeter.jsconst helloWorld = function (name) { process.stdout.write(`hello ${name}!\n`)};const internal = function (name) { process.stdout.write('This is a private function')};exports.sayHello = helloWorld;
By default, these two ...
Read now
Unlock full access