node-env-types v2.0.2
node-env-types
š This package generates the typescript types for environment variables by reading your .env file.
Note: This package also loads newly added environmental variables when you restart your TypeScript server.
Table of Contents
Installation
You can install this package using different package managers as a dev dependency.
Using
yarn:yarn add -D node-env-typesUsing
npm:npm i --save-dev node-env-typesUsing
pnpm:pnpm add -D node-env-typesUsing
bun:bun add -d node-env-types
Usage
After installing this package you can use it as follows:
import load from 'node-env-types'
import process = 'process'
load(process.cwd(), {
filename: ".env", // the path name of the file that contains your environmental variables
});We recommend calling
createEnvTypes(rootPath: string, options?: Options)immediately after your imports. Note thatenv-typeswill be generated after you run the code for the first time, and you must have an.envfile or equivalent in your project.
Console output
The during generation of env-types you can see the output on the console which looks similar to this:
*** loading environment variables from C:\Users\crisp\OneDrive\Documents\npm\node-env-types\.env.
*** created env-types at C:\Users\crisp\OneDrive\Documents\npm\node-env-types\env.d.ts.Parameters
The createEnvTypes function takes two optional parameters:
rootPath- A string indicating the directory path where your.envfile is located. The default value is the current working directory (process.cwd()).options- An optional object of typeOptionscontaining additional configuration options.
Options
| Option | Description | Default Value |
|---|---|---|
filename | This is optional, for example you can load your environmental variables from a file called .env-prod. If not provided the default will be used. | .env |
outputPath | This defines the path where you want your declarative TypeScript file to be output during types generation. | process.cwd() |
Common problems
- Sometimes you may not get auto-completion even if you have generated the
.d.tsfile. All you have to do is to open yourtsconfig.jsonfile, go toincludes, and make sure that your.d.tsfile is listed there. For example, if yourenv.d.tsfile is generated in the root directory, yourincludesarray intsconfig.jsonshould look like this:
{
"compilerOptions": {},
"include": [
"./src/**/*.tsx",
"./src/**/*.ts",
"src/configs/test.ts",
"env.d.ts"
]
}Alternatively you can pass an empty array or point to the root folder of your project instead of src as follows:
{
"compilerOptions": {},
"include": ["./**/*.tsx", "./**/*.ts", "src/configs/test.ts", "env.d.ts"]
}- Before calling the
createEnvTypes()functions make sure that you have a.envfile in your root project of your folder, this is the default filenode-env-typeswill be looking for, If environment variables are named differently, make sure that you specify the correctfilenamein the options.
Languages
This package is intended to be used by developers who codes in typescript.
License
In this package I'm using the MIT license which reads as follows:
MIT License
Copyright (c) 2022 crispengari
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.