Is there something like global functions?

hi, I have functions in javascript file that I want to use in some of my workflows, how can I call the function inside my file without putting another node for it?

I know the I can make require to the file and then call to one of the function, is this the best way?

Hi @Asaf_Shay, you could put your custom code in a single workflow and then call this one “custom code workflow” from all other workflows through the Execute Workflow node.

I don’t want it as workflow, because my workflows have javascript code, so I want to be able to call some function that inside my custom js file

There’s a related Feature Request for this:

You can vote for it.

1 Like

can i make require to js file and call function that inside of him?
maby I can create new module with all my functions and put him in my n8n folder ?

1 Like

That’s possible.

1 Like

I have the custom module
module.exports = {
foo: function () {
// whatever
return “aaa”;
bar: function () {
// whatever
return “bbbb”
I save the file as common.js
when I try to:
var common = require(common.js);
I get the common module not found
where should I put the file ?

nevermind I success to create a custom module and import him and everything is working.

@Asaf_Shay How did you get it working?

I’m facing the same issue. I have a file saved under /data/mytest.js and I’ve tried to import it in a Function node with
const test = require('/data/mytest.js');

But it fails stating

ERROR: Cannot find module ‘/data/mytest.js’

don’t use require when you want to get file, try use “fs” package, it works for me

if you create your own module you need to install it when you install n8n, it looks like this
n8nFolder → npm I …/yourModule
and then you can do, const myModule require(“yourModule”); myModule.myFunction();

1 Like