Languages
[Edit]
EN

Node.js - export function from module

0 points
Created by:
starcraf35
364

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

Below we present how to export a simple function from module1.js to index.js file.

Project structure:

/my-app/
   ├── index.js
   ├── module1.js
   └── package.json

Steps to follow:

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

Practical example:

module.exports = function (text) { 
    console.log(text);
};

2. In index.js file import the function from module1.js using require().

Practical example:

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

text('Some text message...');

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

node index.js

Output:

Some text message...
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.

❤️💻 🙂

Join