[Support Guide] Using environment variables on Netlify correctly

@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.