Environment Variables

Learn about environment variables and how to use them in your projects.

Introduction

Environment variables are a fundamental concept in programming and software development. They are key-value pairs that are stored in the environment that a process runs in. They are often used to store configuration settings and sensitive information like API keys, database passwords, and other secrets.

Most languages have a pre-defined way to access environment variables. Here's a few examples:

API_KEY=your-api-key
import process from "process";
 
const apiKey = process.env.API_KEY; // your-api-key

The key advantage of using environment variables is that they can (and should) be set outside of the application code. This allows them to hold sensitive information without exposing it in the codebase. For this reason, environment variables should never be committed to version control.

Runtime Environment Variables

Sometimes, you may want to set environment variables before or at runtime. This can be done from the command line before running the application, or programmatically at runtime.

Set before running the application
API_KEY=your-api-key node app.js # set before running the application
Set at runtime
import process from "process";
 
process.env.API_KEY = "*your-api-key*"; // set at runtime, anything after this line will have access to the API_KEY

OnlyNv Specifics

OnlyNv offers a suite of extensions to the standard environment variable system.

Environments

OnlyNv allows you to do more than just have a single set of variables in your environment. With prefixes, you can assign multiple values to a single variable name. This allows you to have different values for different environments, such as development, staging, and production.

.env
API_KEY=your-dev-api-key
production:API_KEY=your-actual-api-key

Be Careful

All variables, including those that are environment-specific, are injected into your local runtime. Do not include any production credentials that you are not comfortable with being exposed in your local environment.

When a sync is performed, OnlyNv will automatically inject the correct environment variables based on the current environment. This allows you to easily switch between different environments without having to manually change your environment variables.

Templates

By default, OnlyNv will not persist formatting in your .env file. This means that if you have your variables formatted in a certain way, it will be lost when you sync your environment variables.

In order to persist formatting, you must provide a template file. Usually, this will be a .env.example file that contains the formatting you want to keep. OnlyNv will use this file as a template when syncing your environment variables.

.env
# Default environment variables for local development
 
# API keys
API_KEY=your-dev-api-key
 
# Database connection
DB_HOST=localhost
DB_PORT=5432
DB_USER=devuser
DB_PASS=devpass
 
# Feature flags
FEATURE_X_ENABLED=true

To automatically create a template file, you can use nv strip:

$ nv strip .env > .env.example
.env.example
# Default environment variables for local development
 
# API keys
API_KEY=
 
# Database connection
DB_HOST=
DB_PORT=
DB_USER=
DB_PASS=
 
# Feature flags
FEATURE_X_ENABLED=

By default, OnlyNv will look for template files matching the following patterns:

  • {file}.example
  • {file}.template

On this page