How to pass the host name from env file to nodes?

Hi N8N Team,
How to pass the host name from env file to custom nodes code, can you give any example for this. If this is not possible, can you give me any other approach of how to achieve this.

Regards,
Praveen

Hi praveen,
your custom nodes should have access to Environment Variables using nodejs process.env.

Let’s say you start n8n with env vars using cross-env like this

# start n8n from npm global installation
cross-env MY_HOST_NAME=HOSTNAME n8n start
# or start n8n from github source code
cross-env MY_HOST_NAME=HOSTNAME npm run start

Inside your node’s execute function you should be able to run

console.log('MY_HOST_NAME:', process.env.MY_HOST_NAME); // MY_HOST_NAME: HOSTNAME

If you want to use a .env file to set environment variables you could use the package dotenv-cli like this

# load the variables from the .env file in the current working directory and start n8n
dotenv n8n start
# or 
dotenv npm run start

It might also be interesting for you to look into the environment variable CREDENTIALS_OVERWRITE_DATA in case you want to set predefined credentials using an env var.

2 Likes