← Blog

How seedcord handles environment variables across Node, Workers, dev, and builds

October 18, 2026 · Dhruv · Updated October 24, 2026

I wanted to share how seedcord handles environment variables, because a fair bit happens before a bot gets to do this:

import {  } from 'envapt';

const token = .('DISCORD_BOT_TOKEN');
const token: string | undefined

On Node, that value can come from the process or a .env file. On Cloudflare Workers, it comes from the Worker's env object. During development, the CLI needs some of those values in its own Node process too. Vite also writes some values into the built code, and those have to stay the same after deployment.

And the end goal is simple: the bot author should be able to write the same get calls regardless of the environment. Getting those sources into place, before their code runs, belongs to the framework.

What the application gets

I'll start with what someone writing a bot actually gets out of this. For a bit of context, seedcord supports bots that stay connected to Discord over a websocket, and bots that respond to HTTP requests. The first kind runs on Node. The second can run on Node or Cloudflare Workers.

I use Envapter in both, including inside the framework itself.

The logger, for example, checks Envapter.isDevelopment to choose a default log level. The bot reads its token through envapt too. And someone writing a bot can read their own settings the same way:

const  = .('PORT', ., 3000);
const  = .(
    'COMMAND_GUILD_IDS',
    .(),
    []
);

That means I don't have to add a seedcord option every time someone needs another environment variable. They can add their own validation, use a decorated config class, or change where envapt reads from. It's still their application's configuration.

From my side, I want the logger to keep making that same check on Node and Workers. Where the values come from is something I can set up before the logger runs.

Where envapt reads from

So, where do those values come from?

In envapt, a source is the object that supplies them. On Node, envapt starts with a FileSource, which reads the process environment and loads .env files. I can use that as seedcord's starting point.

Workers need the same envapt reads, but Cloudflare supplies values differently. Importing envapt there selects its portable version. That version starts without a source, so I have to give it one. Cloudflare provides the Worker's configuration and resources through an env object:

import { env } from 'cloudflare:workers';
import { Envapter, PortableSource } from 'envapt';

Envapter.useSource(new PortableSource(env));

PortableSource makes that object available to envapt. If I try to read a variable before calling useSource, envapt throws NoSourceBound. I'll come back to the import order in a bit, because putting this in the wrong file is enough to break startup.

The next thing I need is a way to add values without losing what's already configured. useSource replaces the current source, which is fine when replacement is what I intend. Here, I want to keep it and add Vite's values on top.

That's where Envapter.source, added in envapt 9.2, comes in. I can get the current source and pass it to merge:

Envapter.useSource(
    merge(Envapter.source, new PortableSource(viteValues))
);

Here, viteValues holds the user variables exposed by Vite. I'll get to how seedcord chooses them below.

I don't have to assume the current source is a fresh FileSource. Someone might have supplied their own, or another setup step might have already merged values into it. Envapter.source gives me what's actually configured.

If both sources return a variable through readVars(), the one passed later to merge wins. The merged result also keeps the ability to load files, though envapt allows at most one source that supports files in a merge.

So I can add Vite's values and keep loading .env files through the source that was already doing it. That's the useful bit for me.

A build needs some values to stay put

Okay, but why am I adding Vite's values at all? Here's the behavior I want. Say a project has:

VITE_RELEASE_LABEL=october
API_ORIGIN=https://staging.example.com

By default, Vite exposes VITE_ variables through import.meta.env. When you build, Vite writes those values into the built code. Changing the environment on the server afterward won't change them.

I want someone using envapt to get those same values:

const release = Envapter.get('VITE_RELEASE_LABEL');
const origin = Envapter.get('API_ORIGIN');

Now suppose you build the bot, then change its .env before starting it:

VITE_RELEASE_LABEL=changed-after-build
API_ORIGIN=https://api.example.com

The result, with envapt's default file options, is:

ReadValue in the deployed Node bot
Envapter.get('VITE_RELEASE_LABEL')october

october tells me which release I built. The API address can change depending on where I start that build. Both reads go through envapt.

To get those results, seedcord merges Vite's values into the source it already uses. The later values win. Vite's values win over the ones read when the bot starts. On Node, I keep the existing source and its file support. On Workers, I combine the Worker's bindings with Vite's values.

I still have to choose which value should win. envapt gives me merge to set that order, and calling useSource refreshes its cache so the next read uses the result.

That's the default I chose for seedcord. Someone can deliberately change it: on Node, Envapter.envFileOptions = { override: true } lets .env files replace values from the merged source. They can also replace the source with useSource. I've covered those options in the file options and combining sources guides.

There's one more Vite detail that affects which values reach this merge.

Let Vite decide what Vite exposes

There's a small detail here that matters if you're building on Vite. VITE_ is just the default. Vite lets you change it through envPrefix, including using more than one prefix.

I don't want seedcord to filter for VITE_ again after Vite has already done the filtering. If someone configures APP_, those values should work too.

Instead, I take the object Vite provides and remove its built-in fields:

const {
    MODE: mode,
    BASE_URL: _baseUrl,
    DEV: _dev,
    PROD: _prod,
    SSR: _ssr,
    ...values
} = vite;

I pass values to a PortableSource. I use MODE separately when a Worker hasn't specified its environment; the other built-in fields stay out of envapt.

That gives me the same source in both cases. During dev, I use the values Vite provides for that session. During a build, Vite writes them into the output. Either way, a custom prefix works through the same code.

And yes, those values are readable in the built files. Keep secrets out of the prefixes you expose.

Set up envapt before importing the bot

Now I have the sources I need. I still have to set them up before anything reads from them.

This looks like a reasonable place to do it:

import './bot';

// Set up the environment here.

Except bot runs before the code below that import. If it reads its token as soon as it's imported, I'm already too late. Moving the setup above a static import won't fix it either, because JavaScript still runs the imported module first.

So I generate the entry file in seedcord. On Node, it records where the built bot files are, waits for environment setup, then imports the user's entry. For the environment part, the order is equivalent to:

await import('seedcord:env');
await import('./index.js');

The CLI supplies seedcord:env during dev and build. The bot author doesn't have to create that file. It calls the shared TypeScript function that sets up envapt. The await makes the order explicit.

The Worker entry follows the same order. During dev, I bind the values before the CLI imports the application.

I have to arrange that order in seedcord. envapt reads from the source I give it; I decide when the application's imports run.

The CLI needs the Worker's configuration too

This is the part that's a little less obvious from the bot author's side. Even when their bot runs on a Worker, I still load a copy in Node to generate types and inspect what its files export. The CLI also uses the bot's token for its tunnel and to prevent two dev sessions from starting the same bot.

So I need some of the Worker's configuration in Node too. A token in .dev.vars reaches the Worker through Cloudflare's plugin, but the CLI can't read it through its own envapt source yet.

I read the declared bindings and local secrets for the Node side, set up the Node copy of the bot with those values, and merge the values the CLI needs over its existing source. The Worker environment loader does that work.

I'm only copying configuration here: text, JSON, and local secrets. This doesn't give the Node copy a working KV or D1 service.

Those are Cloudflare resources, so the real Worker keeps using Cloudflare's bindings directly. On the actual Worker, env.KV.get() still accesses KV directly. PortableSource takes a copy of the values for envapt and converts non-string values to JSON when possible. It doesn't modify the original env object, and Envapter.get won't return a KV object with working methods.

I keep those uses separate: envapt reads configuration, and the bot uses Cloudflare's APIs to access its resources.

That covers what I make available to the Node copy. When the dev session ends, I also have to remove those values.

Restoring the source when dev stops

There's one more thing I have to account for in dev. When a deployed bot stops, its process usually exits. In a dev tool, I can stop one bot and start another in the same process.

If I merge one Worker's values into a global source and leave them there, the next session can read them too. If I restore a fresh FileSource, I can throw away a source the application had configured before dev started.

So I save the previous source and restore that exact object when the session ends. Here's the shared function I use in seedcord. It also saves and restores the directory envapt reads files from:

import type { Envapter, Source } from 'envapt';

function bindEnv(
    envapter: typeof Envapter,
    source: Source,
    directory?: string
): Disposable {
    const previousSource = envapter.source;
    const previousBaseDir = previousSource.supportsFiles
        ? envapter.baseDir
        : undefined;
    using cleanup = new DisposableStack();
    cleanup.defer(() => {
        envapter.useSource(previousSource);
        if (previousSource.supportsFiles) {
            envapter.baseDir = previousBaseDir;
        }
    });
    if (previousSource.supportsFiles && directory !== undefined) {
        envapter.baseDir = directory;
    }
    envapter.useSource(source);
    return cleanup.move();
}

I add the returned Disposable to the session's cleanup stack, alongside the servers and the code that loads the bot. When the session stops, the stack restores the configuration too. If setup throws before returning, using cleanup runs the restore inside this function.

The supportsFiles check matters even when I'm just reading baseDir. envapt's portable version warns when I use its file APIs, or throws if fileApiMode is set to 'throw'. I only read or change the directory when the source supports files.

The source and the directory go together: the source determines whether file loading is available, and seedcord chooses which directory to pass to it. In dev, it's the folder containing the project config. A built Node bot uses the recorded config location if the file is still there, or the folder you start it from otherwise. envapt loads the files from whichever directory I pass it.

The cleanup.defer part is probably my favorite bit here. The restore runs as part of stopping the session, including when startup fails halfway through. I can keep it next to the setup instead of adding a reset to every place that stops or restarts a bot.

There's one other choice to explain: which environment the bot uses. That's separate from where it finds its files.

Building in production and starting in staging

Before I finish, I should separate two things that are easy to mix up: the mode I build with, and the environment the bot uses when it starts.

While the CLI runs, I want it and Vite to use the same mode. I use envapt's exported ENVIRONMENT_KEYS to read the requested environment from the shell, check its name, and set Envapter.environment for the CLI. That setter reloads envapt's configuration for the selected environment. Using ENVIRONMENT_KEYS also saves me from keeping my own copy of the order envapt checks them in.

When you start the built Node bot, though, that's a new process. envapt detects its environment there. To start it in production, you can run:

ENV=production node dist/index.mjs

You can start the same build with ENV=staging to load staging's .env files. The values Vite wrote into the build stay the same. That's why import.meta.env.MODE and Envapter.environment can differ in a deployed Node bot.

Node reads its environment when the new process starts. Workers get their values from bindings instead. For Workers, I use the declared environment bindings. If none selects an environment, I fall back to Vite's mode. I supply that fallback through an envapt key reader, so an explicitly configured environment still wins.

Those are the defaults I chose for seedcord. You could make a different choice in your framework, including fixing the bot's environment to the build mode. I've left the details of envapt's detection in the environment guide, including what happens when an environment name is set inside a .env file.

What this leaves the framework to do

That's most of the setup. I still decide where values come from, which ones win, where the files are, and when to set up and restore the source. Those decisions depend on how seedcord starts a bot.

I also keep Discord-specific errors in seedcord. For example, I pass Envapter.get('DISCORD_BOT_TOKEN') to seedcord's token validator so a missing or malformed token gets an error that explains how to fix it.

envapt provides the .env loader, typed reads, converters, validation support, caching, and the code for combining sources. I can use those inside the framework and in the bot without implementing them again in seedcord.

That's why I wanted to write this up. The token read at the top of the post stays the same through all of this, taking away the burden off the user of figuring out where their values come from each time. I set up the source once for each context, and the logger, the bot, and its plugins can read their configuration from there.

If you're making a tool that imports other people's code, or starts their application in more than one environment, I'd start with the same questions. Where do the values come from? Which should win? And when should they stop applying? The Building on envapt guide has shorter examples you can use as a starting point.