node-env-types
v2.0.2
Published
this is a simple package that generates typescript types for .env(dotenv) files.
Downloads
13
Maintainers
Readme
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-types
Using
npm
:npm i --save-dev node-env-types
Using
pnpm
:pnpm add -D node-env-types
Using
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-types
will be generated after you run the code for the first time, and you must have an.env
file 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.env
file is located. The default value is the current working directory (process.cwd()
).options
- An optional object of typeOptions
containing 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.ts
file. All you have to do is to open yourtsconfig.json
file, go toincludes
, and make sure that your.d.ts
file is listed there. For example, if yourenv.d.ts
file is generated in the root directory, yourincludes
array intsconfig.json
should 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.env
file in your root project of your folder, this is the default filenode-env-types
will be looking for, If environment variables are named differently, make sure that you specify the correctfilename
in 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.