Completely lost trying to configure n8n

I’m totally lost on this one - (very much a beginner as well) I’ve installed n8n locally on OSX to build and test some ideas i have. So far its been challenging yet fun and I’m learning a LOT as I go along.
I hit a roadblock a few days ago and have tried to work around it. Roughly explained, I’m trying to build and encode dynamic URLs to post to an external server and have a graph (png) returned. The code all works fine when I test it outside of n8n, but trying to run it in the Function node gives me a “URL is not defined” error. After poking about (and a little help from a mate) I think I understand that URL is an external module and that by default n8n wont allow access to external modules.
Now i’m trying to set up the configuration of n8n so that i CAN access external modules using ‘NODE_FUNCTION_ALLOW_EXTERNAL’ but I have no idea where to set this? I would imagine there is a config file somewhere but I can’t find it.
What am I missing? How can I do this? Am I completely way off?

That is an environment variable. So depends on how you run n8n and on what system. What operating system do you use and do you run via npm or Docker? If via Docker do you run directly or via docker-compose?

I just have a local installation on OSX 11.2.1 - followed the basic installation guide to install ‘globally’ using npm…
Does that mean all i have to do is type:
export NODE_FUNCTION_ALLOW_EXTERNAL=url
In terminal - and it applies to the npm environment?


I’ve tried starting a new Terminal session, typing export NODE_FUNCTION_ALLOW_EXTERNAL=url, then starting n8n from the same terminal window. I still have this error:

ReferenceError: URL is not defined
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:3:15
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:18:4)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1121:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:65:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:492:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:395:62

Can you please post the code of the Function-Node. Because the error message suggests that you have a different problem. If the issue would be that the module can not be loaded, then the error message would say so.

Thanks for helping me jan! I’m very new to all of this and learning as I go. The code I have in the Function node is as follows:

const myUrl = new url("https://image-charts.com/chart");

myUrl.searchParams.append("chd", "a:105.3,102.8,104.9,103.8,101.0,99.8,101.2,98.9,");
myUrl.searchParams.append("chs", "500x300");
myUrl.searchParams.append("cht", "lc");
myUrl.searchParams.append("chtt","test");
myUrl.searchParams.append("chxr", "1,85,110");
myUrl.searchParams.append("chxt", "x,y");

var myNewUrl = myUrl.href;

return [{
    json: {
    myNewUrl
    }
}]

Then multiple things seem to be wrong:

  1. You seem to allow access to the wrong library as the url does not seem to have anything like a searchParams (but did also not look very deep).
  2. You do not load the library. So the first line would have to be something like let url = require('url');

I’ve based what I’ve put together after reading up on url at this website: URL objects
I had tested it within Visualstudio before copying it into n8n and the console.log output gave me exactly what I was after:

https://image-charts.com/chart?chd=a%3A105.3%2C102.8%2C104.9%2C103.8%2C101.0%2C99.8%2C101.2%2C98.9%2C&chs=500x300&cht=lc&chtt=test&chxr=1%2C85%2C110&chxt=x%2Cy

Adding

let url = require(‘url’);

has now given me a different error.

ERROR: Access denied to require ‘url’

VMError: Access denied to require 'url'
    at _require (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/sandbox.js:289:25)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:3:11
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:19:4)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1121:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:65:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:492:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:395:62

Which is what put me on the path of thinking it was a configuration error I had within n8n

Can not test right now but you can try:

export NODE_FUNCTION_ALLOW_BUILTIN=URL

Thanks again Jan - i’m still going around in circles - I’ve tried:

export NODE_FUNCTION_ALLOW_BUILTIN=URL

I typed it in terminal window before typing

n8n start
which hasn’t resulted in anything. I’m going to try installing Docker on my laptop and then installing n8n again to see if that results in something different.

OK could now test.

So it seems like URL is built-in but does apparently get blocked because we sandbox the JavaScript code. So to make it work you have to set:

export NODE_FUNCTION_ALLOW_BUILTIN=url

and then you can do the following:

const URL = require('url').URL;
const myUrl = new URL("https://image-charts.com/chart");
...
1 Like

Perfect! This is it! Thanks for your help Jan.

You are welcome. Great to hear that it helped.

Have fun!

1 Like