'ReactJs Production Code- Uncaught ReferenceError: process is not defined
So I have a bit of an issue with by ReactJs production code where it doesn't seem to recognize my environment variables. This is the error I get:
Uncaught ReferenceError: process is not defined
<anonymous> webpack://testProject/./src/agent.js?:9
js http://localhost:8080/src_Admin_MainPage_SessionTimeout_index_js-src_agent_js-node_modules_moment_locale_sync_recursive_.js:29
__webpack_require__ http://localhost:8080/main.js:2511
fn http://localhost:8080/main.js:2789
<anonymous> webpack://coralpanda/./src/Admin/MainPage/Login/index.js?:5
js http://localhost:8080/src_Admin_MainPage_index_js.js:28
__webpack_require__ http://localhost:8080/main.js:2511
fn http://localhost:8080/main.js:2789
<anonymous> webpack://coralpanda/./src/Admin/MainPage/index.js?:4
js http://localhost:8080/src_Admin_MainPage_index_js.js:128
__webpack_require__ http://localhost:8080/main.js:2511
fn http://localhost:8080/main.js:2789
I followed Webpack's documentation for passing in environment variables and added them to my package.json file's build settings, but it didn't work. Here's how it looks:
"scripts": {
"start": "react-scripts start",
"build": "webpack --mode=production --env REACT_APP_SERVER_URL=https://localhost:3481/project --env NODE_PATH=./src --node-env=production",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"watch": "webpack --watch",
"serve": "webpack serve"
},
And here is my webpack.config.js just in case:
// Generated using webpack-cli https://github.com/webpack/webpack-cli
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const WorkboxWebpackPlugin = require("workbox-webpack-plugin");
const isProduction = process.env.NODE_ENV == "production";
const stylesHandler = isProduction
? MiniCssExtractPlugin.loader
: "style-loader";
const config = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "build"),
},
devServer: {
open: true,
host: "localhost",
},
plugins: [
new HtmlWebpackPlugin({
template: "index.html",
}),
// Add your plugins here
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
],
module: {
rules: [
{
test: /\.(js|jsx)$/i,
loader: "babel-loader",
},
{
test: /\.s[ac]ss$/i,
use: [stylesHandler, "css-loader", "postcss-loader", "sass-loader"],
},
{
test: /\.css$/i,
use: [stylesHandler, "css-loader", "postcss-loader"],
},
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: "asset",
},
// Add your rules for custom modules here
// Learn more about loaders from https://webpack.js.org/loaders/
],
},
resolve: {
alias: {
assets: path.resolve(__dirname, 'src/assets/'),
agent: path.resolve(__dirname, 'src/agent.js'),
"agent.js": path.resolve(__dirname, 'src/agent.js'),
Admin: path.resolve(__dirname, 'src/Admin'),
}
},
};
module.exports = () => {
if (isProduction) {
config.mode = "production";
config.plugins.push(new MiniCssExtractPlugin());
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());
} else {
config.mode = "development";
}
return config;
};
Solution 1:[1]
Fixed the issue. The answer was EnvironmentPlugin. So I had to add that for webpack to recognize my environment variables. I added the following lines to my webpack.config.js:
const { EnvironmentPlugin } = require('webpack')
const config={
plugins:[
new EnvironmentPlugin({
REACT_APP_SERVER_URL: "https://localhost:3481/project",
NODE_PATH: "src/"
})
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nayeer Mahiuddin |
