javascript require and use package code example

Example 1: Node require module

// Use require.main.require to get a module from the root folder.
const Globals = require.main.require("./globals.js");
const YourCustomModule = require("./yourmodule.js");

console.log(YourCustomModule.message);

// --- yourmodule.js ---
module.exports = {
	message: "Hello World!",
	otherData: "Hello Grepper!"
};

Example 2: module wrapper function

Before a module's code is executed, Node.js will wrap it with a function wrapper that looks like the following:

(function(exports, require, module, __filename, __dirname) {
// Module code actually lives in here
});

By doing this, Node.js achieves a few things:

    It keeps top-level variables (defined with var, const or let) scoped to the module rather than the global object.
    It helps to provide some global-looking variables that are actually specific to the module, such as:
        The module and exports objects that the implementor can use to export values from the module.
        The convenience variables __filename and __dirname, containing the module's absolute filename and directory path.