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

@valentino.chiola/safe-vars

v1.0.4

Published

Wrapper for dotenv.config() that parses the result and sets the values in process.env

Downloads

15

Readme

Welcome to @valentino.chiola/safe-vars, a wrapper for dotenv.config() that parses the result and sets the values in process.env

Installation

To install use one of the following commands:

npm i @valentino.chiola/safe-vars
pnpm i @valentino.chiola/safe-vars
yarn add @valentino.chiola/safe-vars

Basic usage

import safeVars, { z } from "@valentino.chiola/safe-vars";

const schema = z.object({
  VARIABLE1: z.string(),
  VARIABLE2: z.string(),
  BOOLEAN: z.coerce.boolean()
});

safeVars(schema)

console.log(process.env)

If in your .env file you have declared VARIABLE1 and VARIABLE2, it won't throw an error.

Note: any extra variables in your .env, that are not present in the schema, will be removed. Note: if the BOOLEAN variable is not defined, zod will cast it to false.

Options

  • inject: If true, the variables will be injected in the process.env object. Default: true
import safeVars, { z } from "@valentino.chiola/safe-vars";

const schema = z.object({
  VARIABLE1: z.string(),
});

export const env = safeVars(schema, { inject: false })

console.log(env.VARIABLE1) // VARIABLE 1
console.log(process.env.VARIABLE1) // undefined
  • log: If true, the variables will be logged in the console. Default: false. Also can be an object with an only property which is an array of the variables that you want to log. Warning: This option should be used for debugging purposes only.
import safeVars, { z } from "@valentino.chiola/safe-vars";

const schema = z.object({
  VARIABLE1: z.string(),
});

safeVars(schema, { log: true })

or:

import safeVars, { z } from "@valentino.chiola/safe-vars";

const schema = z.object({
  VARIABLE1: z.string(),
  VARIABLE2: z.string(),
  SUPER_SECRET: z.string()
});

safeVars(schema, {
  log: {
    only: ["VARIABLE1", "VARIABLE2"],
  },
});
  • throw: If false, it won't throw an error if the variables are not defined in the .env file, but all the variables would be optional. Default: true
import safeVars, { z } from "@valentino.chiola/safe-vars";

const schema = z.object({
  VARIABLE1: z.string(),
});

safeVars(schema, { throw: false });

The type of env.VARIABLE1 will be string | undefined

  • onSuccess: Callback function to be executed if all environment variables are successfully validated.
import safeVars, { z } from "@valentino.chiola/safe-vars";

const schema = z.object({
  VARIABLE1: z.string(),
});

safeVars(schema, {
  onSuccess: (env) => console.log("🚀 Envs loaded!"),
});
  • onError: Callback function to be executed if there are errors in the validation.
import safeVars, { z } from "@valentino.chiola/safe-vars";

const schema = z.object({
  VARIABLE1: z.string(),
});

safeVars(schema, {
  onError: (errors) => console.error("❌ Error loading envs", errors),
});
  • dotenv: An object that represents the options for the dotenv package.

    • path: You can use the path key to determine witch .env file should be parsed. This is useful for testing.
    import safeVars, { z } from "@valentino.chiola/safe-vars";
    
    const schema = z.object({
      VARIABLE1: z.string(),
    });
    
    safeVars(schema, {
      dotenv: {
        path: ".env.test",
      }
    });

Typed Envs

The are two ways to have your envs typed.

  • declare module:
import safeVars, { z } from "@valentino.chiola/safe-vars";

const schema = z.object({
  VARIABLE1: z.string(),
});

safeVars(schema);

declare global {
  module NodeJS {
    interface ProcessEnv extends z.infer<typeof schema> {}
  }
}  
  • return value:

Instead of using process.env you can use the safeVars's return value.

const schema = z.object({
  VARIABLE1: z.string(),
});

export const env = safeVars(schema, { inject: false });

The type of env.VARIABLE1 will be string

Contribution

Contributions are welcome! To contribute:

  1. Fork this repository.
  2. Create a branch with a meaningful description.
  3. Make the desired changes.
  4. Update the documentation if necessary.
  5. Open a Pull Request to the main branch.

If you find an issue or have a suggestion to improve the project, feel free to open an issue.

License

This project is licensed under the MIT License.