npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-env-types

v2.0.2

Published

this is a simple package that generates typescript types for .env(dotenv) files.

Downloads

29

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.

  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.