Node.js - export variable from module

0 points
Created by:

In this article, we would like to show you how to export variable from module in Node.js.

Below we present how to export variable from module1.js to index.js file.

Project structure:

   ├── index.js
   ├── module1.js
   └── package.json

Steps to follow:

1. In module1.js file export the variable using module.exports.

Practical example:

module.exports = 1;

2. In index.js file import the variable from module1.js using require(). You can name it as you want while importing - in our case it will be one variable.

Practical example:

const one = require('./module1');


3. Now you can run the project from terminal to see the output using the following command:

node index.js


Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂