Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment variable for build in Netlify

I'm trying to set an environment variable for an API key that I don't want in my code. My source javascript looks something like this :

.get(`http://api-url-and-parameters&api-key=${process.env.API_KEY}`)

I'm using webpack and the package dotenv-webpack https://www.npmjs.com/package/dotenv-webpack to set API_KEY in a gitignored .env file and it's all running fine on my local. I'd like to also be able to set that variable when deploying through Netlify, I've tried adding it through to GUI to the 'build environment variables', and also to set it directly in the build command, but without success.

Any idea what might be the issue ?

like image 366
Maëlig Avatar asked Jan 25 '18 23:01

Maëlig


People also ask

How do I set environment variables in Netlify?

Site settings > Build & deploy > Environment > Environment variables , to be exact, ooh or Team settings > Sites > Global site settings > Shared environment variables if you're the "sharing is caring" type. These can also be set using the Netlify configuration file, netlify. toml .

How do you set a Build command in Netlify?

Basic build settings. With continuous deployment configured, you can specify how Netlify will build your site by going to Site settings > Build & deploy > Continuous Deployment > Build settings. From Build settings, you can set a base directory, add a build command, and specify a publish directory.

What is the use of .ENV file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.


2 Answers

WARNING: If this is a secret key, you will not want to expose this environment variable value in any bundle that gets returned to the client. It should only be used by your build scripts to be used to create your content during build.

Issue

dotenv-webpack expects there to be a .env file to load in your variables during the webpack build of your bundle. When the repository is checked out by Netlify, the .env does not exist because for good reason it is in .gitignore.

Solution

Store your API_KEY in the Netlify build environment variables and build the .env using a script prior to running the build command.

scripts/create-env.js

const fs = require('fs')
fs.writeFileSync('./.env', `API_KEY=${process.env.API_KEY}\n`)

Run the script as part of your build

node ./scripts/create-env.js && <your_existing_webpack_build_command>

Caveats & Recommendations

  • Do not use this method with a public facing repository [open] because any PR or branch deploy could create a simple script into your code to expose the API_KEY
  • The example script above is for simplicity so, make any script you use be able to error out with a code other than 0 so if the script fails the deploy will fail.
like image 194
talves Avatar answered Sep 21 '22 03:09

talves


You can set Dotenv-webpack to load system environment variables as well as those you have declared in your .env file by doing the following:

  plugins: [
    new Dotenv({
        systemvars: true
    })
  ]

I.e Setting the systemvars attribute of your webpack dotenv plugin to true.

Note that system environment variables with the same name will overwrite those defined in your .env file.

Source: https://www.npmjs.com/package/dotenv-webpack#properties

like image 38
Riky_Tree Avatar answered Sep 21 '22 03:09

Riky_Tree