Set environment variable for build in Netlify

后端 未结 6 1011
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 05:52

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-ur         


        
相关标签:
6条回答
  • 2020-12-03 06:07

    What you can also do is also to define a global constant in Webpack. Netlify environment variables defined in UI will work with it. You don't need dotenv or dotenv-webpack.

    webpack.config.js

    const webpack = require("webpack");
    
    module.exports = {
      plugins: [
        new webpack.DefinePlugin({
          "process.env.API_KEY": JSON.stringify(process.env.API_KEY)
        }),
      ]
    }
    

    However again, of course you shouldn't do it just inputting enviornmental variables in the frontend if your API key is confidential and project public. The API key will appear in the source code of the website and will be easily accessible for everyone visiting it. Lambda function would be a better option.

    0 讨论(0)
  • 2020-12-03 06:09

    if you go to corresponding site's settings in Netlify, under build&deploy you can find a section called environment variables you can easily add your environment variables from there. if you add MY_API_KEY variable to environment variables you will be able to access it inside your project via process.env.MY_API_KEY.

    0 讨论(0)
  • 2020-12-03 06:21

    If you're using Nuxt JS there is a more "straight forward" approach.

    Just edit the nuxt.config.js like so:

    module.exports = {
      env: {
        GOOGLE_API_KEY: process.env.GOOGLE_API_KEY
    
      },
      // ...
    

    Then add the GOOGLE_API_KEY to Netlify through the build environment variables as usual.

    Credit goes to yann-linn and his answer on github.

    0 讨论(0)
  • 2020-12-03 06:24

    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.
    0 讨论(0)
  • 2020-12-03 06:28

    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

    0 讨论(0)
  • 2020-12-03 06:31

    You can use the Netlify's config file also ... You can find documentation here. Also i wanted to have the same ENV variables with with different values per branch/environment. This workaround worked for me:

    Create a netlify.toml file like:

    [build]
      NUXT_ENV_BASE_API = "/api"
      NUXT_ENV_HOST_DOMAIN = "https://your-domain.gr"
    
    [context.branch-deploy]
      environment = { NUXT_ENV_BASE_API = "/dev-api", NUXT_ENV_HOST_DOMAIN = "https://dev.your-domain.gr" }
    
    [context.production]
      environment = { NUXT_ENV_BASE_API = "/api", NUXT_ENV_HOST_DOMAIN = "https://your-domain.gr" }
    

    And deploy in Netlify ...

    0 讨论(0)
提交回复
热议问题