Use `now.json` environment variables while developing
Clone or download
sergiodxa and timothyis Remove package.json support (#15)
* Remove package.json support

* Document CLI usage
Latest commit 638b5b8 Nov 22, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Remove package.json support (#15) Nov 22, 2018
lib Remove package.json support (#15) Nov 22, 2018
.gitignore Use Yarn instead Jan 17, 2018
LICENSE Remove package.json support (#15) Nov 22, 2018
index.js Remove package.json support (#15) Nov 22, 2018
package.json 3.1.0 Jun 21, 2018
readme.md Remove package.json support (#15) Nov 22, 2018

readme.md

now-env

With the help of this package, you can easily set environment variables for the use in development.

If you're already using a now.json file, the env sub property will be assigned to process.env automatically.

In addition, you can store secrets locally, that are specific to the development environment.

IMPORTANT: Do not use this package in production. It should only be used locally, while developing. This is because the secrets that you're using locally shouldn't match the ones you use in production (more secure). In production, you should be adding secrets like this.

Usage

Firstly, install the package from npm:

yarn add now-env

As the last step, register the helper by loading it:

require('now-env')

That's all, you can now check process.env for the environment variables!

If your application is running inside Now cloud then this module is not going to do anything and let Now set your environment variables.

If using this with Next.js, follow the example at next.js/examples/with-now-env to make sure now-env only runs on the server at build time.

Secrets

Most probably you will want to use secret keys in your now.json file. This module allow you to use them too without worries in development.

Create a now.json with some secret defined as @secret-name, similar to:

{
  "env": {
    "SECRET": "@my-secret-key",
    "ANOTHER_SECRET": "@my-other-secret-key",
    "SECRET_FAIL": "@this-is-not-defined"
  }
}

Then create a now-secrets.json with the secrets names and values.

{
  "@my-secret-key": "keep-it-secret",
  "@my-other-secret-key": "keep-it-secret-too"
}

This file must be ignored to actually keep them secret.

Then when starting your application now-env will read the now.json and get the values from now-secrets.json. If a environment key can't be found in now-secrets.json (or the file doesn't exists) then is going to use the secret name as value, that means if DB_PASS is @db_pass and you don't define it inside now-secrets.json then the value will be @db_pass.

Required Variables

Now supports using the env key as an array of required values you'll need to provide when deploying. This module also allows you to use them in development.

Create a now.json with the array, similar to:

{
  "env": [
    "REQUIRED_KEY",
    "REQUIRED_SECRET"
  ]
}

Then create a now-required.json with the environment keys and values.

{
  "REQUIRED_KEY": "required-value",
  "REQUIRED_SECRET": "@required-secret"
}

You can also use secrets, for that you will need to create a now-secrets.json too.

Then when starting your application now-env will read the now.json and get the values from now-required.json (and now-secrets.json). If a environment key can't be found in now-required.json then is going to throw a reference error.

CLI Usage

Since now-env should only run in development it makes sense to add it as a devDependency in yourr application package.json. In that case you may not want to add require('now-env') to your code since it will break in case you only install production dependencies.

You can solve this using now-env only via the CLI with the following command:

node -r now-env index.js

Now Node.js will require now-env before running your index.js. If you define it as a dev script in the package.json you could have a start script just running node index.js and not using now-env at all!

Migrating from dotenv

If you're already using the dotenv module you can switch to now-env easily:

  1. Create a now.json file with the env key with every environment variable
  2. Install now-env
  3. Change require('dotenv').config() with require('now-env')
  4. That's it!