Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How pass argument from one nodejs file to another?

Tags:

import

node.js

I have two node js file "a and b", and i want to pass parameter from from file a.js to file b.js and also return the result of b.js to a.js

like image 981
sachin murali Avatar asked Oct 29 '25 07:10

sachin murali


2 Answers

You should create a function in file a.js and export it and call this function from the file b.js and pass the value in the arguments.

In file a.js

function myFunction(value){
//in value you will get your passes value
}
exports.myFunction = myFunction

In file b.js

const fileA = require('./a')
fileA.myFunction('you value') 
like image 138
Arvind Rajput Avatar answered Oct 30 '25 23:10

Arvind Rajput


You can do this by including one file to another, For Example in file "a.js" you can create a method that takes an argument as input and process it and then return back the result which then can be used by file "b.js". This two way communication can be handled through function call, please review the following code.

File a.js

function funcA(input)
{
console.log(">> Input Taking");
console.log(input);
var result = 1; 
return result;

}

exports.funcA = funcA;

File b.js

const myFileA = require('./a');
var response = myFileA.funcA("John Doe");
console.log(response);
like image 41
Usama Ayaz Avatar answered Oct 30 '25 23:10

Usama Ayaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!