Node.js | Call function from original file in imported file - javascript

I recently tried to organize my code by exporting functions into different files, so my main file stays clean. To do so I imported the other js file in my main file with
const request = require('./request.js');
In "request.js" I used export, so I can call the function in my main file. Is it is possible to call a function, that is defined in the main file, from the "request.js" file? Unfortunately I can't just return the information back to the main file, because I am using callbacks.

Yes it's possible.
You just need to export function from the main file and then import your main file inside request.js:
// main.js:
module.exports.someFunction = function() {
// Some code
};
const request = require('./request.js');
request.someRequestFunction();
// request.js
var main = require('./main.js');
module.exports = {
someRequestFunction: function() {
main.someFunction(); // Call function from main module
}
};

Related

can i use normal javascript code if in that code imported some module

So in my code it gives me error FunctionOfMainJs is not defined which is function made of main.js so what can i do for run normal js code and also use import in same file
main.js
import {CreateDropDown} from "./asset/js/customdropdown.js";
new CreateDropDown([{"ImagePath":null,"OptionValue":"text1"}],"Container");
function FunctionOfMainJs(){
alert("hey guys");
}
FunctionOfMainJs();
Are you trying to call this function in another .js file? If so you will need to export the function:
module.exports = {FunctionOfMainJS}

Firebase functions - multiple js files to organize functions

I have a folder named functions, inside there is index.js.
On index.js I have the main function that accept all http :
exports.contentServer = functions.https.onRequest((request, response) => {
Since I have many more functions I need to organize my files, and I want to add another file, say payments.js, so that index.js can call functions inside payments.js, or even onCreate callbacks will be fired from payment.js.
If I just create another js file in functions, it won't work.
What has to be done to be able to have a single Cloud Function in another new js file ?
You can use a standard nodejs require to load code in other files.
index.js
const otherFunctions = require('./other.js');
exports.contentServer = functions.https.onRequest((request, response) => {
otherFunctions.other()
}
other.js
exports.other = function() { ... }
I suggest reading the documentation for more information.

NodeJS Module Exports with Additional Required Files

I am trying to break my nodejs code up into as small of files as possible to keep things modular. I have an app.js that is needing to use another method that I have moved to utils.js. The utils file however has a method that has a requirement for another library.
Should my utils.js file be "requiring" dependencies or should all those go in my app.js?
// app.js
var utilities = require('./utilities');
..
return utilities.anotherMethod();
// utils.js
module.exports = {
producer: function () {
// Handle mapping of fields depending on the source system
return 'map fields'
},
anotherMethod: function () {
// I require another lib. Do I do that here or app.js?
var kafka = require('kafka-node');
}
};
Update: Regarding the close request for an opinion based answer is essentially telling me that this can be done either way, which is what I was trying to clarify.

Referenced Module cannot be found (Javascript)

I have defined a function and put it into a separate file (util.js) in a util folder. I then export the function within the module.
I then require the module in the main function and then invoke the function in the main function.
However the lambda console always prompts that it is unable to find the module "util/util.js"
I have tried everything I can think of and hope that you can help me getting things back on track.
The util.js (placed in a folder named "util":
module.exports.generateText = generateText
function generateText() {
console.log("Function invoked")
}
The main function requiring the util.js and invoking the function:
const referenced = require("util/util.js")
exports.handler = (event, context, callback) => {
// invoke the function in the util.js
referenced.generateText()
}
I just would like to call the function in the util/util.js file from the main function.
To import a local module, you need to prefix the path with "./": const referenced = require("./util/util.js")

How do I export 2 methods from a single server side module (js file)?

I'm using Node/Express.
I have a server, index.js. I have a different js module, called validmoves.js.
Typically, I can export the single function I define in a module by saying:
module.exports = shuffleFunction;
And then require it in the express server file, index.js, by saying:
let shuffle = require('./routes/shuffleRoute');
How can I export 2 function from the module? One of them is actually a helper function.
You can define a name of the module you're exporting and specify on the import. That would look something like this:
// shuffleRoute.js
module.exports.shuffleFunction = shuffleFunction;
module.exports.someHelperFunction = someHelperFunction;
and in your other file:
// index.js
let shuffle = require('./routes/shuffleRoute').shuffleFunction;
let helper = require('./routes/shuffleRoute').someHelperFunction;
You can export multiple functions from a module:
module.exports.shuffleFunction = shuffleFunction
module.exports.somethingElse = somethingElse
Use it in index.js:
let shuffle = require('./route/shuffleRoute');
shuffle.shuffleFunction();
shuffle.somethingElse();
See the example here.

Categories