Skip to content

Bun

For the most part, Varlock just works with Bun the same way it works with Node.js, and other JavaScript integrations work the same way.

Bun does its own automatic loading of .env files, based on the current value of NODE_ENV (or BUN_ENV), which it defaults to development if not set. This causes problems when bun decides to load .env.development and passes those env vars into varlock.

The best way to fix this is to disable bun’s automatic loading of .env files in your bunfig.toml file:

bunfig.toml
env = false

You may also use the --no-env-file CLI flag when invoking scripts with bun/bunx.

Note that if you are building a standalone executable using bun build, you can use the --no-compile-autoload-dotenv flag to disable this behavior in the final executable.

bun build --compile bundles the varlock/auto-load runtime code into your executable, but it does not bundle the separate Varlock CLI that resolves your config. If the executable resolves config when it starts, your deployment must also include the installed varlock package and its dependencies. The executable looks for node_modules/.bin/varlock in its directory and each parent directory.

For example, this monorepo layout keeps the CLI available to the compiled server:

apps/server/dist/server
apps/server/node_modules/.bin/varlock

If your deployment platform installs or prunes dependencies, declare varlock as a production dependency in a package whose node_modules directory is deployed with the executable.

You can also launch the executable through the CLI. This resolves the config before the application starts, so the bundled varlock/auto-load code reuses the injected environment instead of looking for the CLI again:

Terminal window
varlock run -- ./dist/server

If your deployment otherwise contains only the compiled executable, install the standalone Varlock binary in the deployment environment and launch the application through varlock run.

One option we have with bun is to use a preload script, configured in bunfig.toml. If you do this, you will no longer have to use bun run varlock run -- yourscript or use import 'varlock/auto-load' in your code!

bunfig.toml
preload = ["varlock/auto-load"]