Environment Variables

Your can use process.env.NODE_ENV in the app code to tell whether it's a production build or not. We also allow you use consume custom environment variables from .env files.

if (process.env.NODE_ENV === 'production') {
  console.log('this is the production build!')
}

Adding Environment Variables In .env

To define permanent environment variables, create a file called .env in the root of your project:

POI_APP_SECRET_CODE=abcdef

Poi loads .env files by this order:

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

Notably:

  • Files on the top have more priority than files on the bottom.
  • mode is set using the CLI flags. It defaults to development but you can use --prod, --production (which sets it to production mode) and --test (which sets it to test mode).

WARNING

You must create custom environment variables beginning with POI_APP_. Any other variables except NODE_ENV will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. Changing any environment variables will require you to restart the development server if it is running.

Adding Temporary Environment Variables In Your Shell

You can start the development server with a temporary variable defined.

First install cross-env so you can define env variables cross-platform:

yarn add cross-env --dev

Then in your npm scripts:

{
  "scripts": {
    "start": "cross-env POI_APP_SECRET=abcdef poi --serve"
  }
}

In this case, your variable name also MUST start with POI_APP_ otherwise it will not be embeded in your app code.

Adding Environment Variables In Poi Config File

You can also use envs option in poi.config.js to define permanent environment variables:

module.exports = {
  envs: {
    __VERSION__: '0.0.0'
  }
}

In this way, your variable name doesn't have to start with POI_APP_.

Last Updated: 11/28/2018, 4:57:30 PM