node js require path 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: node.js name of file

var path = require('path');
var scriptName = path.basename(__filename);

Example 3: npm can i use my modules without specifying the path

npm install --save ../path/to/mymodule