无法在客户端上使用来自 NPM 的 React,因为未定义 'development'。该包是从 Webpack 生成的
React from NPM cannot be used on the client because 'development' is not defined. The bundle was generated from Webpack
我正在创建一个 React Node.js 应用程序,我正在尝试生成一个 Webpack 包,其中包含我从 NPM 加载的 React 源代码。
不过,客户端好像不能直接使用npm的react代码。它触发此错误:
Uncaught ReferenceError: development is not defined
触发异常的代码来自React代码:
有什么我可以做的吗?
编辑
这是我的 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')
]
};
我的client.js
文件只包含这一行(为了调试这个问题):
import React from 'react';
作为字符串传递给 webpack.DefinePlugin
的任何值都被视为代码片段——也就是说,使用
new webpack.DefinePlugin({
ENV: "development"
});
用代码
console.log(ENV);
结果
console.log(development);
相反,您想要
new webpack.DefinePlugin({
ENV: "\"development\""
});
这将导致
console.log("development");
要解决您的问题,请将您的插件更改为
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': "'development'"
}
}),
new ExtractTextPlugin('[name].css')
]
我通常允许 webpack 从 process.env.NODE_ENV
读取,这样当你 运行 webpack 使用 NODE_ENV=production
:
时,React 会正确缩小
new webpack.DefinePlugin({
"process.env": { NODE_ENV: JSON.stringify(process.env.NODE_ENV || "development") }
})
我正在创建一个 React Node.js 应用程序,我正在尝试生成一个 Webpack 包,其中包含我从 NPM 加载的 React 源代码。
不过,客户端好像不能直接使用npm的react代码。它触发此错误:
Uncaught ReferenceError: development is not defined
触发异常的代码来自React代码:
有什么我可以做的吗?
编辑
这是我的 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')
]
};
我的client.js
文件只包含这一行(为了调试这个问题):
import React from 'react';
作为字符串传递给 webpack.DefinePlugin
的任何值都被视为代码片段——也就是说,使用
new webpack.DefinePlugin({
ENV: "development"
});
用代码
console.log(ENV);
结果
console.log(development);
相反,您想要
new webpack.DefinePlugin({
ENV: "\"development\""
});
这将导致
console.log("development");
要解决您的问题,请将您的插件更改为
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': "'development'"
}
}),
new ExtractTextPlugin('[name].css')
]
我通常允许 webpack 从 process.env.NODE_ENV
读取,这样当你 运行 webpack 使用 NODE_ENV=production
:
new webpack.DefinePlugin({
"process.env": { NODE_ENV: JSON.stringify(process.env.NODE_ENV || "development") }
})