Access window object inside execute method

How can I access the hostname on which n8n is running? window.location.host doesn’t seem to work. I tried changing target in tsconfig.json and also tried adding dom as value of lib in compilerOptions. But, while executing the workflow, I get error “window is not defined”.

Hey @krisk918,

I have to ask… What is it you are trying to do? As a quick test I have found you can use {{location.hostname}} but I am not sure if that would actually work on a scheduled workflow.

2 Likes

For some node communicating with a MQTT broker, I need to use different hostname for MQTT broker based on whether it is a dev machine or a VM instance. Dev machine is windows which uses WSL 2, where I need to use host.docker.internal for localhost.

Hi @krisk918, welcome to the community!

The workflows are running server-side and the window object would not be available in Node.js (since there is no browser window). However, when deploying n8n, you can set the N8N_HOST variable.

You can then read this variable in your workflows using an expression like {{$env["N8N_HOST"]}}. Would this work for you?

image

Of course, this would also work with any other environment variable in case you need a different format or anything.

3 Likes

Yes, this works. Thanks.

1 Like

Glad to hear, thanks for confirming! It would be awesome if you could mark this question as solved to make it easier for peeps to identify open topics.

2 Likes