{"id":13836103,"url":"https://github.com/fastify/fastify-env","last_synced_at":"2025-05-15T02:06:50.146Z","repository":{"id":25244257,"uuid":"103541354","full_name":"fastify/fastify-env","owner":"fastify","description":"Fastify plugin to check environment variables","archived":false,"fork":false,"pushed_at":"2025-05-01T06:32:41.000Z","size":179,"stargazers_count":219,"open_issues_count":1,"forks_count":26,"subscribers_count":18,"default_branch":"main","last_synced_at":"2025-05-01T07:31:55.760Z","etag":null,"topics":["fastify","fastify-plugin"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@fastify/env","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastify.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"fastify","open_collective":"fastify"}},"created_at":"2017-09-14T14:18:18.000Z","updated_at":"2025-05-01T06:32:38.000Z","dependencies_parsed_at":"2023-01-14T02:23:30.130Z","dependency_job_id":"3053377f-7332-4acc-9ed3-d5a79b3959bc","html_url":"https://github.com/fastify/fastify-env","commit_stats":{"total_commits":189,"total_committers":26,"mean_commits":7.269230769230769,"dds":0.7142857142857143,"last_synced_commit":"f5aad072fb7116932c5503c79e7a8278a9233a95"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastify","download_url":"https://codeload.github.com/fastify/fastify-env/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254259370,"owners_count":22040819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastify","fastify-plugin"],"created_at":"2024-08-04T15:00:35.669Z","updated_at":"2025-05-15T02:06:50.126Z","avatar_url":"https://github.com/fastify.png","language":"JavaScript","readme":"# @fastify/env\n\n[![CI](https://github.com/fastify/fastify-env/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-env/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/env.svg?style=flat)](https://www.npmjs.com/package/@fastify/env)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nFastify plugin to check environment variables\n\n## Install\n\n```\nnpm i @fastify/env\n```\n\n### Compatibility\n\n| Plugin version | Fastify version |\n| ---------------|-----------------|\n| `\u003e=5.x`        | `^5.x`          |\n| `^4.x`         | `^4.x`          |\n| `\u003e=2.x \u003c4.x`   | `^3.x`          |\n| `\u003e=0.x 2.x`    | `^2.x`          |\n| `\u003e=0.x 2.x`    | `^1.x`          |\n\n\nPlease note that if a Fastify version is out of support, then so are the corresponding versions of this plugin\nin the table above.\nSee [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.\n\n## Usage\n\n```js\nconst fastify = require('fastify')()\nconst fastifyEnv = require('@fastify/env')\n\nconst schema = {\n  type: 'object',\n  required: [ 'PORT' ],\n  properties: {\n    PORT: {\n      type: 'string',\n      default: 3000\n    }\n  }\n}\n\nconst options = {\n  confKey: 'config', // optional, default: 'config'\n  schema: schema,\n  data: data // optional, default: process.env\n}\n\nfastify\n  .register(fastifyEnv, options)\n  .ready((err) =\u003e {\n    if (err) console.error(err)\n\n    console.log(fastify.config) // or fastify[options.confKey]\n    console.log(fastify.getEnvs())\n    // output: { PORT: 3000 }\n  })\n```\n\nYou can also use the function `getEnvs()` of the Request from within a handler function:\n```js\nfastify.get('/', (request, reply) =\u003e {\n    console.log(request.getEnvs())\n    // output: { PORT: 3000 }\n})\n```\nNote that the `getEnvs` decorators will not be added if they already exist.\n\nThis module is a wrapper around [env-schema](https://www.npmjs.com/package/env-schema).\nTo read a `.env` file you must set `dotenv` in the options:\n\n```js\nconst options = {\n  dotenv: true // will read .env in root folder\n}\n\n// or, pass config options available on dotenv module\nconst options = {\n  dotenv: {\n    path: `${__dirname}/.env`,\n    debug: true\n  }\n}\n\n```\n### Using @fastify/env to configure other plugins\nThe `@fastify/env` plugin loads asynchronously. If you wish to use its values in a different plugin before the boot sequence, you need to make sure that:\n1. `@fastify/env` is registered first.\n2. Await the plugin registration or await after()\n\n```js\nawait fastify.register(fastifyEnv)\n// fastify.config can be used in here\n```\n\nOR\n```js\nfastify.register(fastifyEnv)\nawait fastify\n// fastify.config can be used in here\n```\n**NB** Support for additional properties in the schema is disabled for this plugin, with the `additionalProperties` flag set to `false` internally.\n\n### Typescript\nTo have typings for the fastify instance, you should either:\n\n- use the `declaration merging` technique to enhance the `FastifyInstance` type with the property and its keys you have defined in the options:\n\n```typescript\ndeclare module 'fastify' {\n  interface FastifyInstance {\n    config: { // this should be the same as the confKey in options\n      // specify your typing here\n      FOO: string\n    };\n  }\n}\n\nconst fastify = Fastify()\nfastify.register(fastifyEnv)\n\nfastify.config.FOO // will be a string\nfastify.config.BAR // error: Property BAR does not exist on type { FOO: string }\n```\n\n- use the generic function `getEnvs()` to get the already typed object:\n\n```typescript\ntype Envs = {\n  FOO: string\n}\n\nconst fastify = Fastify()\nawait fastify.register(fastifyEnv)\n\nconst envs = fastify.getEnvs\u003cEnvs\u003e() // envs will be of type Envs\n\nenvs.FOO // will be a string\nenvs.BAR // error: Property BAR does not exist on type Envs\n```\nIf this is the case it is suggested to use [json-schema-to-ts](https://github.com/ThomasAribart/json-schema-to-ts) to have the type always synchronized with the actual schema.\n\n## Acknowledgments\n\nKindly sponsored by [Mia Platform](https://www.mia-platform.eu/).\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","funding_links":["https://github.com/sponsors/fastify","https://opencollective.com/fastify"],"categories":["\u003ch2 align=\"center\"\u003eAwesome Fastify\u003c/h2\u003e"],"sub_categories":["\u003ch2 align=\"center\"\u003eEcosystem\u003c/h2\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastify%2Ffastify-env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-env/lists"}