/.env
โโโโREACT_APP_.. should start in REACT_APP_ for enviroment variable
using in anywhere
โโโโprocess.env.REACT_APP_
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_
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)
โโโโโโ ],
โโโโโโ }
โโโโโโ };