Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
/ netlify-env Public archive

Resolve environment variables from netlify.toml, locally

Notifications You must be signed in to change notification settings

zanona/netlify-env

Repository files navigation

netlify-env

Build Status Maintainability Test Coverage

Resolve environment variables from netlify.toml, locally.

Netlify uses netlify.toml file allowing developers to set env variables dependant on context. However, when developing locally we would like to make sure to use the same variable set available from that file. For this reason, this package was created to help maintain the same workflow as we would expect when running from Netlify. The package will look for the closest netlify.toml file related to the current process directory and expand those variables so it can be sent to the running script

Install

$ npm install --save netlify-env
$ yarn add --save netlify-env

Usage

const env = require('netlify-env');

env({context: 'staging', file: 'config.toml'}).then(vars => {
  console.log(vars); // object containing all available env vars
});

API

env({context, file});

context

Type: String

Override current server or local context variables with the ones defined under a specific context. Accepted values are: production, deploy-preview, branch-deploy, or development which is a custom context created for developing locally.

Defaults to development.

file

Type: String

Use a specific TOML file to parse environment variables from. The program will look for the closest top file available, so you can run anywhere in your project, even if the file is on the root directory.

Defaults to netlify.toml.

CLI

  $ netlify-env --context deploy-preview --file config.toml

https://github.com/zanona/netlify-env/blob/master/cli.js#L21-L41

Example response:

NODE_ENV=staging
LOG_LEVEL=2
API_ENDPOINT=https://api.example.com/staging/get-keys

Application on package.json

{
  "name": "my-website",
  "version": "0.0.0",
  "scripts": {
    "build": "export $(netlify-env) && build-my-website",
    "test": "export $(netlify-env -c development) && ava"
  }
}

License

MIT © Marcus Zanona

Releases

No releases published

Packages

No packages published