node-env-types
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

node-env-types

🌀 This package generates the typescript types for environment variables by reading your .env file.

logo

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.

  1. Using yarn:

    yarn add -D node-env-types
  2. Using npm:

    npm i --save-dev node-env-types
  3. Using pnpm:

    pnpm add -D node-env-types
  4. 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 that env-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:

  1. rootPath - A string indicating the directory path where your .env file is located. The default value is the current working directory (process.cwd()).

  2. options - An optional object of type Options 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 your tsconfig.json file, go to includes, and make sure that your .d.ts file is listed there. For example, if your env.d.ts file is generated in the root directory, your includes array in tsconfig.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 file node-env-types will be looking for, If environment variables are named differently, make sure that you specify the correct filename 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.

Package Sidebar

Install

npm i node-env-types

Weekly Downloads

1

Version

2.0.2

License

MIT

Unpacked Size

84.1 kB

Total Files

33

Last publish

Collaborators

  • crispengari