I'm creating a React Node.js app and I'm trying to generate a Webpack bundle containing the React source code I loaded from NPM.
However, it seems that the React code from NPM cannot be used directly in the client. It triggers this error:
Uncaught ReferenceError: development is not defined
The code that triggers the exception is from the React code:
Is there anything I can do to make that work?
EDIT
This is my webpack.config.js
:
import _ from 'lodash';
import webpack from 'webpack';
import yargs from 'yargs';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
export default {
entry: {
bundle: './src/client.js'
},
output: {
filename: '[name].js',
path: './dist/assets',
publicPath: '/assets/'
},
externals: undefined,
resolve: {
extensions: ['', '.js', '.json']
},
module: {
loaders: [
{test: /\.js/, loader: 'babel?optional=es7.objectRestSpread!client', exclude: /node_modules/ },
{test: /\.css/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")},
{test: /\.less$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader")},
{test: /\.json$/, loader: 'json'},
{test: /\.jpe?g$|\.gif$|\.png$/, loader: 'file?name=[name].[ext]'},
{test: /\.eot$|\.ttf$|\.svg$|\.woff2?$/, loader: 'file?name=[name].[ext]'}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': 'development'
},
'development': true
}),
new ExtractTextPlugin('[name].css')
]
};
My client.js
file only contains this line (for the purpose of debugging this issue):
import React from 'react';
And here is the resulting bundle
To solve the "Uncaught ReferenceError: process is not defined" in React, open your terminal in your project's root directory and update the version of your react-scripts package by running npm install react-scripts@latest and re-install your dependencies if necessary.
Any values passed to webpack.DefinePlugin
as strings are treated as code fragments—that is to say, using
new webpack.DefinePlugin({
ENV: "development"
});
with the code
console.log(ENV);
results in
console.log(development);
Instead, you want
new webpack.DefinePlugin({
ENV: "\"development\""
});
which will result in
console.log("development");
To fix your issue, change your plugins to
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': "'development'"
}
}),
new ExtractTextPlugin('[name].css')
]
I usually allow webpack to read from process.env.NODE_ENV
so that React minifies properly when you run webpack with NODE_ENV=production
:
new webpack.DefinePlugin({
"process.env": { NODE_ENV: JSON.stringify(process.env.NODE_ENV || "development") }
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With