How do I properly configure environment variables for Netlify?

  • 0
    Hello everyone, I am unable to set the environment variables for running the application on Netlify. I've added all the variables to Site Settings / Environment, but either that's not enough, or I've configured something wrong. Everything works locally, in dev and prod modes.

    PS Before that, I ran a project on create-react-app, and everything worked out of the box there, so there is a suspicion that the matter is in my web pack config.

    package.json
    "scripts": {
        "build": "yarn run clean-dist && webpack -p --config=webpack.prod.js",
        "clean-dist": "rimraf dist/*",
        "lint": "eslint './src/**/*.{js,ts,tsx}' --quiet",
        "start": "yarn run start-dev",
        "start-dev": "webpack-dev-server --config=webpack.dev.js --open",
        "start-prod": "yarn run build && node express.js",
        "test": "jest --coverage --watchAll --config=configs/jest.json"
      },


    webpack.dev
    const {resolve} = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
    const Dotenv = require('dotenv-webpack');
    
    module.exports = {
      output: {
        path: resolve(__dirname, 'dist'),
        filename: 'index_bundle.js',
        publicPath: '/'
      },
      resolve: {
        plugins: [new TsconfigPathsPlugin({ configFile: "tsconfig.json" })],
        extensions: ['.ts', '.tsx', '.js', '.jsx'],
      },
      context: resolve(__dirname, './src'),
      module: {
        rules: [
          {
            test: /\.js$/,
            use: ['babel-loader', 'source-map-loader'],
            exclude: /node_modules/,
          },
          { 
            test: /\.tsx?$/, 
            loader: "ts-loader" 
          },
          {
            test: /\.css$/,
            use: ['style-loader', { loader: 'css-loader', options: { importLoaders: 1, modules: true, } }],
          },
          {
            test: /\.(jpe?g|png|gif|svg)$/i,
            loaders: [
              'file-loader?hash=sha512&digest=hex&name=img/[hash].[ext]',
              'image-webpack-loader?bypassOnDebug&optipng.optimizationLevel=7&gifsicle.interlaced=false',
            ],
          },
        ],
      },
      plugins: [
        new HtmlWebpackPlugin({template: 'index.html.ejs',}),
        new Dotenv(),
      ],
      externals: {
        'react': 'React',
        'react-dom': 'ReactDOM',
      },
      performance: {
        hints: false,
      },
      node: {
        fs: 'empty',
      }
    };


    webpack.prod
    const merge = require('webpack-merge');
    const {resolve} = require('path');
    
    const commonConfig = require('./webpack.common');
    
    module.exports = merge(commonConfig, {
      mode: 'production',
      entry: './index.tsx',
      output: {
        filename: 'js/bundle.[hash].min.js',
        path: resolve(__dirname, './dist'),
        publicPath: '/',
      },
      devtool: 'source-map',
      plugins: [],
    });
    React Anonymous, Jun 3, 2019

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!