require object javascript 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: javascript require
// The require() method is used to load and cache JavaScript modules.
// So, if you want to load a local, relative JavaScript module into a Node.js application,
// you can simply use the require() method.
// Example:
var yourModule = require( "your_module_name" ); //.js file extension is optional
Example 3: How to get the path to the file that required your module?
module.parent