fastify-env
Fastify plugin to check environment variables
Install
npm install --save fastify-env
Usage
const fastify = require('fastify')()
const fastifyEnv = require('fastify-env')
const schema = {
type: 'object',
required: [ 'PORT' ],
properties: {
PORT: {
type: 'string',
default: 3000
}
}
}
const options = {
confKey: 'config',
schema: schema,
data: data
}
fastify
.register(fastifyEnv, options)
.ready((err) => {
if (err) console.error(err)
console.log(fastify.config)
})
This module is a wrapper around env-schema.
To read an .env
file you must set dotenv
in the options:
const options = {
dotenv: true
}
const options = {
dotenv: {
path: `${__dirname}/.env`,
debug: true
}
}
NB Support for additional properties in the schema is disabled for this plugin, with the additionalProperties
flag set to false
internally.
Typescript
In order to have typing for the fastify instance, you should follow the example below:
declare module 'fastify' {
interface FastifyInstance {
config: {
};
}
}
Acknowledgements
Kindly sponsored by Mia Platform