@wolfstar/env-utilities loads layered dotenv files and provides strict parsers for application configuration.
Load with stars.config
With compatibility version 5, the default,stars loads your environment for you. When your project depends on @wolfstar/env-utilities, stars calls setup() as the very first import of the built entry, before plugin registrations and your own modules. Projects that build with tsdown or Vite no longer need to call setup() by hand.
Pass setup() options through the env option of stars.config:
stars.config.ts
env: falseturns the automatic registration off. Projects that build withtsc, and JavaScript projects, setenv: falseand keep callingsetup()themselves.env: true, or any option, turns it on.- The registration stays off when
experimental.enableNitrois set.
Load Dotenv Files
Outside the automaticstars.config registration, use the side-effect entry point when the default .env path is enough:
setup:
.env.local is intentionally skipped in the test environment.
Choose a Loader
setup() needs no extra package. It picks a loader in this order:
varlock: when the project has a varlock schema (src/.env.schema,.env.schema, or avarlock.loadPathinpackage.json).dotenv: when the optionaldotenvanddotenv-expandpeer dependencies are installed.node: otherwise. It parses files with Node.js’util.parseEnvand requires Node.js 20.12 or newer.
loader option or DOTENV_LOADER to choose one yourself. See Loaders for details.
How the CLI follows varlock
Withfuture.compatibilityVersion 6, the stars CLI follows the runtime when it picks varlock by itself. The CLI treats the loader as varlock when all of these are true:
- The project depends on
@wolfstar/env-utilities. - The project has a varlock schema (
src/.env.schema,.env.schema, or avarlock.loadPathinpackage.json). varlockis installed.stars.configsets neitherenv.pathnorenv.loader.
stars dev and stars doctor then read your variables through varlock load, the way the bot does, instead of the dotenv files. dev.url no longer takes its port from those dotenv files either.
stars prepare, stars build, stars dev, and stars info print VARLOCK_LOADER_IMPLICIT when the CLI detects varlock but stars.config does not say so. Run stars doctor --fix to add env: { loader: 'varlock' } for you:
stars.config.ts
stars doctor for the checks it runs.
Type Application Keys
Augment the package’sEnv interface so parsers accept your keys:
Env from the types varlock generates instead of declaring each key again:
env export of @wolfstar/env-utilities/varlock. See With a varlock schema.
Parse Values
ReferenceError. Invalid values throw TypeError. A provided default is returned as-is.
Configure the Loader Itself
You can setDOTENV_PATH, DOTENV_ENV, DOTENV_PREFIX, DOTENV_ENCODING, DOTENV_DEBUG, and DOTENV_LOADER before setup. Keep secrets
outside source control and configure them through your deployment platform.