dotenvrc

Read .envrc

Usage no npm install needed!

<script type="module">
  import dotenvrc from 'https://cdn.skypack.dev/dotenvrc';
</script>

README

dotenvrc

dotenvrc loads environment variables from a .envrc file into process.env.

Motivation

In short, I wanted jest to use environment variables written in a .envrc.

At the writing moment, jest seems to run in a sandboxed environment, i.e. it omits runtime environment variables. Since I'm a big fan of The Twelve-Factor App and direnv, I wanted to keep the single source of truth theory.

Goal

To have the same result of what direnv does in popular and/or realistic use cases.
In this sense, function or shell command execution would be out of support.

Features

  • local variable assignment
  • Supports export
  • Supports export -n (un-export)
  • Supports several backslash notations
    • \xXX: ASCII hex code
    • \uXXXX: 4 digits unicode
    • \UXXXXXXXX: 8 digits unicode
    • \n, \r, \t, \v, \b, \a
  • Supports parameter expansion
    • $VAR
    • ${VAR}
  • Especially treats $PWD. $PWD is expanded to the directory where .envrc found.

Not supported

  • Other than a simple variable assignment and export.
    • Shell command execution
    • Arithmetic expansion
    • Shell history expansion
    • A series of variable calculation e.g. ${#var}, ${var:-val}, etc.

Install

npm install dotenvrc

Usage

To inject .envrc content into process.env, simply:

require('dotenvrc');

If you have .env file instead of .envrc, the following might work for you. (It might not since the Parsing rules are different.)

require('dotenvrc/dotenv').inject()

Example .envrc file

# Firebase/GCP
export BOTO_CONFIG=$PWD/.boto
export GOOGLE_APPLICATION_CREDENTIALS=$PWD/secret/goog-credencials.json

# Cloud Datastore emulator
PORT=18081
export DATASTORE_EMULATOR_PORT=$PORT
export DATASTORE_EMULATOR_HOST=http://localhost:$PORT

LICENSE

MIT