Environment Variables

Already Defined Environment Variables

There're already defined environment variables you can access in both Node.js process and your app code:

  • POI_COMMAND: The current CLI command you're running.
  • POI_MODE: The internal mode for current CLI command. The value is always one of production development and test.

And the one only defined for app code: NODE_ENV whose value is either production or development which is inferred from POI_MODE. If you want to define NODE_ENV in the Node.js process, run the commands with a temporary environment variable:

# macOS / linux
NODE_ENV=production poi build

# Or cross-platform
yarn add cross-env --dev
cross-env NODE_ENV=production poi build

WARNING

The NODE_ENV variable you defined in the Node.js process won't affect the one we use in your app code which is always inferred from POI_MODE.

Adding Environment Variables In .env

Poi will load environment variables in .env files to Node.js process:

FOO=bar
POI_APP_SECRET=secret

But only the variables starting with POI_APP_ will be embeded in your app code. Any other variables will be ignored to avoid accidentally exposing a private key on the machine that could have the same name.

What other .env files can be used?

  • .env: Default.
  • .env.local: Local overrides. This file is loaded for all environments except test.
  • .env.{NODE_ENV}: Environment-specific settings.
  • .env.{NODE_ENV}.local: Local overrides of environment-specific settings.

Files on the bottom have more priority than files on the top.

WARNING

As we mentioned before, Poi does NOT define NODE_ENV in the Node.js process. If you want environment-specific settings, run the commands with a temporary NODE_ENV variable, for example: cross-env NODE_ENV=production poi builld will use .env.production.local .env.production .env.local and .env.

Expanding Environment Variables In .env

Expand variables already on your machine for use in your .env file (using dotenv-expand).

For example, to get the environment variable npm_package_version which is available when running Poi in npm scripts:

POI_APP_VERSION=$npm_package_version
# also works:
# POI_APP_VERSION=${npm_package_version}

Or expand variables local to the current .env file:

DOMAIN=www.example.com
POI_APP_FOO=$DOMAIN/foo
POI_APP_BAR=$DOMAIN/bar
Last Updated: 11/9/2018, 7:57:08 AM