Fastify plugin to check environment variables
npm install --save fastify-env
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', // optional, default: 'config'
schema: schema,
data: data // optional, default: process.env
}
fastify
.register(fastifyEnv, options)
.ready((err) => {
if (err) console.error(err)
console.log(fastify.config) // or fastify[options.confKey]
// output: { PORT: 3000 }
})
This module is a wrapper around env-schema so, to read .env
file you must set the dotenv
in options:
const options = {
dotenv: true // will read .env in root folder
}
// or, pass config options avalible on dotenv module
const options = {
dotenv: {
path: `${__dirname}/.env`,
debug: true
}
}
NB: internally this plugin force to not have additional properties, so the additionalProperties
flag is forced to be false
In order to have typing for the fastify instance, you should follow the example below.
declare module 'fastify' {
interface FastifyInstance {
config: { // this should be same as the confKey in options
// specify your typing here
};
}
}
Kindly sponsored by Mia Platform