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

@brodo/json_env

v1.3.0

Published

Loads an environment variables from JSON files (`.env.json` per default) and starts a subprocess with them.

Downloads

21

Readme

json_env

Environment Variable Loader

npmjs.com Crates.io License

json_env is dotenv, but with JSON. It loads an environment variables from JSON files (.env.json per default) and starts a subprocess with them. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.

How to install

With homebrew:

$ brew tap brodo/json_env
$ brew install json_env

With NPM

$ npm i -g @brodo/json_env

With cargo:

$ cargo install json_env

Or download the binaries for your platform on the releases page and put them in your $PATH.

How to use

Just run json_env with any program as a parameter:

$ json_env my_program

Additional command line arguments that are passed to json_env are forwarded to the child process:

$ json_env echo "Test"

Test

Example

.env.json:

{
    "NODE_ENV": "DEV",
    "MY_USER": "Carl",
    "NUM_USERS": 10,
    "nested": {
        "hello": "world",
        "boo": "far"
    }
}

Shell:

$ json_env env
MY_USER=Carl
NODE_ENV=DEV
NUM_USERS=10
nested={"boo":"far","hello":"world"}
[...]

Environment Variable Expansion

You can include existing environment variables in your env file to expand them:

.env.json:

{
  "MY_VAR": "$FOO",
  "MY_OTHER_VAR": "User:$USER"
}

Shell:

$ json_env -e env
FOO=Bar
MY_OTHER_VAR=User:Carl
MY_VAR=Bar
USER=Carl
[...]

JSON Path support

There are some use cases where you already have environment variables defined in a JSON file but not at the root level. Take this Azure Function local.settings.json file for example:


{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "<language worker>",
    "AzureWebJobsStorage": "<connection-string>",
    "MyBindingConnection": "<binding-connection-string>",
    "AzureWebJobs.HttpExample.Disabled": "true"
  },
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "*",
    "CORSCredentials": false
  },
  "ConnectionStrings": {
    "SQLConnectionString": "<sqlclient-connection-string>"
  }
}

The Values property contains the environment variables we are interested in. You can use this file to run app.js with the environment variables defined in Values by providing the JSON Path .Values using the -p flag:

$ json_env -c local.settings.json -p .Values node app.js

Using multiple config files

In some cases, it makes sense to use several config files. For example, one file can be checked into your VCS containing default values and a second one can be used to overwrite some of them:

defaults.json

{
  "SERVER_URL": "https://example.com/foo",
  "USER": "TO_BE_OVERWRITTEN",
  "PASSWORD": "TO_BE_OVERWRITTEN"
}

my_settings.json

{
  "USER": "admin",
  "PASSWORD": "hunter2"
}

Use multiple -c flags to add several config files:

$ json_env -c defaults.json -c my_settings.json env
PASSWORD=hunter2
SERVER_URL=https://example.com/foo
USER=admin
[...]

Later config files overwrite the earlier ones. You can also use multiple JSON paths, which are applied in order.

License

json_env is licensed under the Apache 2.0 license.