Using project ENV file in Execute Command

Describe the problem/error/question

I’m trying to execute a local script with NodeJS using the Execute Command node. The issue I’m having is that it’s not including my env variables from the local project, which I have set in the project’s .env file and is normally being added from Dotenv.

I’m not sure what happens when I run it from n8n, but it doesn’t receive the variables & crashes the script. I also have console.log commands in the script and I don’t see any logs in the browser or the n8n source (NPM & Docker w/ Bind mounts). I added the NODE_FUNCTION_ALLOW_BUILTIN=* and NODE_FUNCTION_ALLOW_EXTERNAL=* flags in the latest build and I’m still not seeing results.

I’ve searched around a lot and I haven’t found any references to my issue. Anybody know why this is occurring and/or how I might debug?

What is the error message (if any)?

Command failed: npx tsx "/home/node/engineering/Business/Eight or Infinity/VZBZ/app.ts" /home/node/engineering/Business/Eight or Infinity/VZBZ/node_modules/discord.js/src/client/Client.js:216 if (!token || typeof token !== 'string') throw new DiscordjsError(ErrorCodes.TokenInvalid); ^ Error [TokenInvalid]: An invalid token was provided. at Client.login (/home/node/engineering/Business/Eight or Infinity/VZBZ/node_modules/discord.js/src/client/Client.js:216:52) at (/home/node/engineering/Business/Eight or Infinity/VZBZ/app.ts:31:8) at ModuleJob.run (node:internal/modules/esm/module_job:234:25) at async ModuleLoader.import (node:internal/modules/esm/loader:473:24) at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:122:5) { code: 'TokenInvalid' } Node.js v20.18.2

Please share your workflow

I run the Execute Command using the Test Step button:
npx tsx "/path/to/app.ts"

app.ts:

import { tagStarUser } from './functions/star';
import { Client, Events, GatewayIntentBits } from 'discord.js';
import type { Message } from 'discord.js';
import dotenv from 'dotenv';

dotenv.config();
console.log('env', process.env)
console.log('bot token', process.env.BOT_TOKEN)
console.log('starboard', process.env.STARBOARD)
const { BOT_TOKEN, STARBOARD, DROID_TERMINAL } = process.env;
const { ClientReady, MessageCreate } = Events;

const {
  Guilds, GuildMessages, MessageContent,
  GuildMessageReactions, GuildMembers
} = GatewayIntentBits;

const client = new Client({
  intents: [
    Guilds, GuildMessages, MessageContent, GuildMessageReactions, GuildMembers
  ]
});

client.once(ClientReady, readyClient => {
  console.log(`${readyClient.user.username} Initialized`)
});

client.login(BOT_TOKEN)

Information on your n8n setup

  • n8n version: 1.78.0
  • Database: default
  • n8n EXECUTIONS_PROCESS setting: default
  • Running n8n via: Docker & npm
  • Operating system: Win10

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

I managed to make it work by passing the directory to the execution file and getting the .env

import { fileURLToPath } from 'url';
import { dirname } from 'path';
import path from 'path';
import dotenv from 'dotenv';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
dotenv.config({ path: path.join(__dirname, '.env') });