Categories
JavaScript Answers

How to get a variable from a file to another file in Node.js?

Spread the love

To get a variable from a file to another file in Node.js, we use modules.

For instance, we write

module.exports = { clientIDUnsplash: "SuperSecretKey" };

to export an object with the clientIDUnsplash property in unsplash.js.

Then we write

const { clientIDUnsplash } = require("./unsplash");

to call require to require the unsplash.js file and get the clientIDUnsplash property from the returned object.

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 *