[Support Guide] Using environment variables on Netlify correctly

A post was split to a new topic: API keys and environment variables on netlify

Hi @bswank,

During the netlify build process you can both modify your function and access en vars, which means you can use a custom webpack plugin, script, etc. to hardcode the environment variables you need in to the functions the same way React automatically hardcodes en vars that start with REACT_APP_ in to the client side javascript. You’ll have to do it yourself but it’s definitely possible.

thanks futuregerald. If I’ve create an environment variable (GAME_ID) in settings ( https://app.netlify.com/sites/sss-phaser/settings/deploys) , then how do I access that variable in my webpack.config.js? Specifically I want to use the environment variable in the entry property:

module.exports = (env = {}) => {

console.log(“env $$$$$$$”, env);
console.log(“process.env $$$$$$$”, process.env);
console.log(“GAME_ID”, GAME_ID)

return {
entry: env.production ? /* ./src/games/${env.GAME_ID}.js */ ‘./src/boilerplate/game.ts’ : ‘./src/boilerplate/game.ts’, // ‘./src/games/asteroid/game.ts’,

There are probably a million ways, but when I need to populate a file with a value, I do something like this in my build command:

sed -i s/PLACEHOLDER/${VARIABLENAME}/g webpack.js && my-usual-build-command

This way the value is put in place in the file before I need it. I don’t know how webpack runs things in its config file while you use it, so there may be a more elegant way but it comes back down to “populate the value in a file before you need it”.

thanks greatly for your help

I have added the snippet to my build command in my package.json:

“scripts”: {
“build”: “sed -i s/PLACEHOLDER/${GAME_ID}/g webpack.config.js && webpack --mode=production --env.production”

and the build & deploy run successfully. But my webpack.config.js still cannot see the variable:

6:22:59 PM: > sed -i s/PLACEHOLDER/${GAME_ID}/g webpack.config.js && webpack --mode=production --env.production
6:22:59 PM: env $$$$$$$ { production: true }
6:22:59 PM: env.GAME_ID $$$$$$$ undefined

the webpack site has a page about environment variables:

it shows that if I define module.exports with a function that takes an env argument then I will have access to the env variable inside the body of the function.

So I do this in my webpack.config.js but there is no sign of my GAME_ID environment variable value:

module.exports = env => {

console.log(“env $$$$$$$”, env);
console.log(“env.GAME_ID $$$$$$$”, env.GAME_ID);

@mcferren, my understanding is that package.json doesn’t not allow for the expansion of environment variables:

Including this in package.json in this way won’t work work because environment variables won’t be e.

The example that @fool gave is invoking a command to do a find/replace of the two strings in webpack.js. This is just an example so show what is possible. In that solution the command would replace the literal string “PLACEHOLDER” with whatever text was in the environment variable - in that file: webpack.js.

This is one of many way to copy the environment variable into the actual file itself. The data on disk is being changed. There are other ways to take an environment variable and include in the the text of a file. This was just one easy way to do so.

The solution here will require having some reproducible way to take an environment variable in your local environment and then getting that environment variable working in your local build. If you can get that working, we can help you adapt it to Netlify (if it doesn’t “just work” - which it might).

Do have the inclusion of the environment variable working locally? If so, how do you do so there? With this information we can hopefully suggest a way to do the same at Netlify.

In most cases, the environment variable should be available to node as process.env.NAME_HERE (replacing NAME_HERE with the name of the environment variable).

Thy key caveat is that the environment variable only exists in the build system. If you want that same environment variable to exist in the site javascript, your build code will need to copy this environment variable to the javascript files being generated by the build process.

Dear respected @luke @fool Kindly help me out: I have passed the locally a Environment Variable (snap:https://www.screencast.com/t/QKgXWi56P), now I want to follow these steps
to pass the Environment Variable at aap.netlify.com i.e.(snap:https://www.screencast.com/t/07krkPkmmB) in the code and steps how do I achive/pass
dynmically aap.netlify.com Environment Variable in the code

Local steps followed. at windows machine

1st step install:: phaser3-typescript> npm install dotenv
2nd step: create a .env file i.e: phaser3-typescript> type .env
3rd step: edit the code in Webpak.config.js

var path = require("path");
var pathToPhaser = path.join(__dirname, "/node_modules/phaser/");
var phaser = path.join(pathToPhaser, "dist/phaser.js");
const env = require("dotenv").config();
module.exports =  (env) => {


 entry: "./src/games/" + process.env.GAME_ID +"/game.ts",
  output: {
	path: path.resolve(__dirname, "dist"),
	filename: "bundle.js"
  module: {
	rules: [
	  { test: /\.ts$/, loader: "ts-loader", exclude: "/node_modules/" },
	  { test: /phaser\.js$/, loader: "expose-loader?Phaser" }
  devServer: {
	contentBase: path.resolve(__dirname, "./"),
	publicPath: "/dist/",
	host: "",
	port: 8080,
	open: true
  resolve: {
	extensions: [".ts", ".js"],
	alias: {
	  phaser: phaser

4th step: in pakage json file :: add command calling object:::
“scripts”: {
“snake”: “webpack --env.GAME_ID=snake --mode development && webpack-dev-server --mode development”,

5th setep: run your command in CLI mode :: i.e: phaser3-typescript> npm run snake

Doesn’t really sound like a netlify question, @davis - how you handle things locally would be up to you. We can’t provide in-depth support for your use of webpack or dotenv We can just assert that if you set a variable here:


…then it is exposed during your build process (or while you run netlify dev)

So if you want to try removing dotenv and webpack from the equation and set the environment variable there in our UI, and use a test that is more straightforward like a build command of:


…and don’t see it working, do let me know and I’ll be happy to try to help sort things out.

Hello. I am having trouble how to use my .env variables on Gatsby with Netlify.
I have a API that works in development and production.
In development it is being called with SERVER_HOST_ROOT
which I am using to make api calls through the site.

However, in production when I build the site and deploy to Netlify the enviroment variable is gone!

Not sure what I am doing wroing


Hi @aquasar!

How are you trying to use the env var? The .env file won’t just work on its own, as explained in the above article.

I could use some help on this topic.

Here’s what I’ve done so far.

  1. Created an app that needs to read in an API key.
  2. Created a an environment variable for it in my Netlify Build & Deploy > Environment settings.
  3. Created a Netlify (AWS) Lambda function to expose my environment variable from step 2.

This works great so far. When I hit the endpoint for my Lambda function, it displays my API key in a webpage.

But, I need to access this key in my server.js. Sorry for the “noob” question, how to read my API key env var in my server.js file? What am I missing?

Any help is greatly appreciated! Thanks!

Cross-posted from here.

What I did was perform the action needs inside the lambda function directly. Here’s an example I made: token-hider-inator. Hope that helps.

1 Like

Thanks @Dennis, I think I actually came across your code during my research. I like what you’ve done. I will keep it as a reference.

For my case, I was working on legacy. So, I didn’t want to copy the entire function into my lambda. My exercise was more academic anyway. I just wanted to learn the full life cycle and document it for future reference. Which I did here https://gist.github.com/marklchaves/7d664f71062447189f4e423d28925bc3

Thanks again! :slight_smile:

Would be great if @fool and/or his compadres could give some hints on using the Netlify env vars (as set in the GUI) to feed the token needed for webmentions. I’m stumped. Am using Eleventy + webpack. At least, after finding this thread, now I know why the combo of an .env file, dotenv, and dotenv-webpack isn’t working in Netlify. The repo currently is private but may not stay that way, so the otherwise excellent answer given by @talves won’t work for me.

Thanks in advance for any help from whoever wishes to give it.

Hmm, I don’t see anything on that page you linked that requires a token. Could you clue me in as to what the use case is in more detail?

1 Like

TL;DR: Turned out not to be a Netlify issue, after all.

I was trying to send an API token from an env var so webmentions would work. When I would do a build, would see a line saying that the token wasn’t being read (from my .env file). That’s when I found the advice to send it through Netlify env var settings instead. I thought, when I originally sent this request for info, that it still wasn’t getting through — but later saw that it was, after all, and another unrelated glitch (PEBKAC) was the reason why I wasn’t seeing webmentions. So, yes, the Netlify method is still needed and it is working, but there was PEBKAC going on, too. :roll_eyes: (I did respond to one of your team via Twitter about this but I guess it didn’t get to you, too.) Thanks!

1 Like

I’m not sure if it is addressed in this topic discussion, but I had an issue in that the creds I needed where in JSON format, and that they are typically read from a file. Since I don’t want to check in or deploy a credentials file I was stuck.

What I discovered was that I can set an env var with a LARGE JSON string, then parse that as the credentials object. There is an limit on env var values, so beware.


I have a question regarding environment variables and forked pull requests

Imagine I have a private API key set as an environment variable in the project’s Netlify UI, something like MY_KEY=abc123

My GitHub repository is public and the Netlify project is public, so logs are visible to everyone

First question: if I do env as a build command, will MY_API value be printed in the logs, or do you hide sensitive variables by default? Like other CIs replace env values with “***” strings.

Second question: someone forks my repo, changes the build command to be “env” and submits a pull request. Will Netlify pass the environment variables to the forked PR?

Hi @bahmutov, environment variables are not currently masked in the deploy log and could potentially be exposed in a forked PR. We are currently working on a solution for this that we plan to release very soon.


Hi, and if I understand from looking at the user interface, there is no way to disable building pull requests from forked repositories?