Categories
JavaScript Answers

How to use global variable in Node.js?

Spread the love

To use global variable in Node.js, we create a module.

For instance, we write

module.exports = new Logger(customConfig);

in logger.js to export the Logger object.

Then we write

const logger = require("./logger");
logger.log("foo");

in app.js to call require to import the logger.js module and call logger.

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 *