Try โ€‚HackMD Logoโ€‰HackMD

Set environment variable reactjs and nextjs

Reactjs

/.env

โ€‹โ€‹โ€‹โ€‹REACT_APP_..   should start in REACT_APP_ for enviroment variable

using in anywhere

โ€‹โ€‹โ€‹โ€‹process.env.REACT_APP_

Nextjs

https://nextjs.org/docs/basic-features/environment-variables

/.env

โ€‹โ€‹โ€‹โ€‹NEXT_PUBLIC_..   should start in NEXT_PUBLIC for enviroment variable

/.env.local

Don't need to start with NEXT_PUBLIC_..

In order to expose a variable to the browser you have to prefix the variable with NEXT_PUBLIC_

using in anywhere

โ€‹โ€‹โ€‹โ€‹process.env.NEXT_PUBLIC_

Non-create-react-app

client

  • Using dotenv in client with server (setting webpack.config.js)
    https://trekinbami.medium.com/using-environment-variables-in-react-6b0a99d83cf5

  • (on bottom this page)
    https://stackoverflow.com/questions/42182577/is-it-possible-to-use-dotenv-in-a-react-project

  • my example (setting https://github.com/jherr/wp5-starter-react template)

    client/webpack.config.js

    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  const HtmlWebPackPlugin = require("html-webpack-plugin");
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  const deps = require("./package.json").dependencies;
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  //setting for .env
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  const webpack = require('webpack');
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  const dotenv = require('dotenv');
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  module.exports =()=> {
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    // call dotenv and it will return an Object with a parsed key 
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    const env = dotenv.config().parsed;
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    // reduce it to a nice object, the same as before
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    const envKeys = Object.keys(env).reduce((prev, next) => {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      prev[`process.env.${next}`] = JSON.stringify(env[next]);
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      return prev;
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    }, {});
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  return{
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    output: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      publicPath: "http://localhost:8080/",
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    },
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    resolve: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      extensions: [".jsx", ".js", ".json"],
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    },
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    devServer: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      port: 8080,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    },
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    module: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      rules: [
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          test: /\.m?js/,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          type: "javascript/auto",
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          resolve: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            fullySpecified: false,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          test: /\.css$/i,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          use: ["style-loader", "css-loader"],
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          test: /\.(js|jsx)$/,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          exclude: /node_modules/,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          use: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            loader: "babel-loader",
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        //custom setting (for font .ttf load)
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          test: /\.(woff2?|eot|ttf|otf)$/,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          use: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            loader: "url-loader",
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            options: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹              name: "[name].[ext]?[hash]",
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      ],
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    },
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    plugins: [
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      new ModuleFederationPlugin({
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        name: "starter",
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        filename: "remoteEntry.js",
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        remotes: {},
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        exposes: {},
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        shared: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          ...deps,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          react: {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            singleton: true,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            requiredVersion: deps.react,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          "react-dom": {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            singleton: true,
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            requiredVersion: deps["react-dom"],
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        },
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      }),
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      new HtmlWebPackPlugin({
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        template: "./src/index.html",
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      }),
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      new webpack.DefinePlugin(envKeys)
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    ],
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    }
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  };
    

server