Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 25 days ago by NovaSatellite675

How can n8n's Execute Command node include local .env variables for a NodeJS script?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I’m running a NodeJS script with n8n’s Execute Command node, and my project’s .env file (loaded via dotenv) isn’t being applied, causing the script to crash due to a missing or invalid bot token.

I execute the script using the following command:

BASH
npx tsx "/path/to/app.ts"

Here’s the content of my app.ts file:

TYPESCRIPT
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)

The script fails with this error message:

BASH
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

I have also added the NODE_FUNCTION_ALLOW_BUILTIN=* and NODE_FUNCTION_ALLOW_EXTERNAL=* flags to my build, but the variables still aren’t loaded. I haven’t found references to this issue elsewhere. Any guidance on how I can pass my .env variables to the script when running it inside n8n, or tips on how to debug this problem would be greatly appreciated.

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

Comments 0

Answers 0

Views 21

0 Answers

Discussion

No comments yet.