Fastify does not provide any built-in handling of env vars, although there is a fastify-env plugin, which internally uses dotenv files with json schema and ajv for validations. Whether you use that plugin, or have wired up something else yourself, we think using DMNO instead is worth the additional benefits.
Installation
While dmno init
will automatically detect that you are using Fastify and install the necessary packages for you, you may also want to install them yourself:
The rest of the Fastify setup looks slightly different depending on if you areusing the fastify-cli or not. Select your situation using the tabs below:
Initialize + register dmnoFastifyPlugin
Wherever you initialize your fastify
instance and register plugins, import and register our dmnoFastifyPlugin
:
Create a new file in the plugins directory to initialize the plugin:
Adjust your package.json scripts
In this case, we must run our dev
and start
commands via dmno run
. You’ll want to adjust your package.json
scripts accordingly. Your existing scripts may not match exactly, but that’s ok. Just note that if you want live reload you need to include the -w
flag.
Configure your configuration schema
dmno init
will scaffold out the schema
in your config.mts
files based on your existing .env
files. See our Schema Guide for the specifics of how to author additional updates to your DMNO schema
.
Accessing config
Use
DMNO_CONFIG
instead ofprocess.env
🎉
You’ll now have fully typed and validated config and some cool security features described below.
Security and leak prevention
Aside from the general DX improvements that DMNO provides, it also introduces important security features to keep your secrets safe:
- redacts your sensitive config from logs
- intercepts requests that send sensitive config to hosts not on an allow list
- stops returning sensitive config as part of server responses
You can read more about these features and how to enable/disable them in our Security Guide.
The Fastify plugin does its best to enable these things automatically, but it would be entirely reasonable to disable these features, and use the underlying helpers to customize the behavior.