How Can i Run Any JavaScript in n8n code

how can i make n8n run any javascript i want

Describe the issue/error/question

this is the script for example n8n code cant run

var xhr = new XMLHttpRequest();
xhr.open(“GET”, “https://bigchunges.net/api/v1/user/daily?t=” + Date.now(), true);
xhr.setRequestHeader(“Authorization”, “Bearer token”);
xhr.send();

What is the error message (if any)?

ERROR: XMLHttpRequest is not defined [line 1]

Share the output returned by the last node

ReferenceError: XMLHttpRequest is not defined
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:1:108
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:6:2
    at VM2 Wrapper.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/bridge.js:485:11)
    at Sandbox.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/nodevm.js:426:23)
    at Sandbox.runCodeAllItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Sandbox.js:36:42)
    at Sandbox.runCode (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Sandbox.js:30:62)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:75:39)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:658:51)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:585:68
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Information on your n8n setup

  • n8n version: latest
  • Database you’re using (default: SQLite): latest
  • Running n8n with the execution process [own(default), main]: own
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: docker

Any idea how to run any javascript code i want in n8n like i am running it from console

Welcome to the community @CyberGeek.

That is sadly not possible. JavaScript code in n8n runs in the backend, so Node.js which is very different to running JavaScript in the browser. There is for example no window. as Node.js does not have a window and so on.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.