JavaScript Answers

How to add global variables for Node.js standard modules?

Spread the love

To add global variables for Node.js standard modules, we export the variables.

For instance, we write

const common = {
  util: require("util"),
  fs: require("fs"),
  path: require("path"),

module.exports = common;

to export the common object by setting it as the value of module.exports in common.js.

Then we write

const common = require("./common.js");

to call require to include the exports in common.js in our code.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *