Set environment variable for build in Netlify

后端 未结 6 1119
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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.

提交回复
热议问题