EN
Node.js - export object from module
0 points
In this article, we would like to show you how to export object from module in Node.js.
Below we present how to export object from module1.js to index.js file.
Project structure:
xxxxxxxxxx
1
/my-app/
2
├── index.js
3
├── module1.js
4
└── package.json
Steps to follow:
1. In module1.js file export the object using module.exports
.
Practical example:
xxxxxxxxxx
1
module.exports = {
2
name: 'Tom',
3
age: 23
4
}
2. In index.js file import the object from module1.js using require()
.
Practical example:
xxxxxxxxxx
1
const user = require('./module1');
2
3
console.log(user.name);
4
console.log(user.age);
3. Now you can run the project from terminal to see the output using the following command:
xxxxxxxxxx
1
node index.js
Output:
xxxxxxxxxx
1
Tom
2
23