I'd like to load and execute a Javascript function in external Javascript file using Node.js and similarly retrieve back with desired result after execution. Is it possible/not possible? If not, any other alternatives.
Any help regarding this is greatly appreciated.
Yes, you can load another JS file as a module using Node.js module system and function
require
. There's quite good description of how to do it in Node.js documentation, take a look int this page.Basically you have to load your external file calling function
require
and passing path to that file:but to make this work you have to export that function by writing something like
in your external file, where
functionToExport
is the name of the function which you want to use.