0
我遇到了一個我認爲來自webpack的錯誤。那就是:ReferenceError:全局未定義在eval
index.js:9 Uncaught ReferenceError: global is not defined
at eval (index.js:9)
at Object.<anonymous> (bundle.js:2548)
at __webpack_require__ (bundle.js:622)
at fn (bundle.js:48)
at eval (client:1)
at Object.<anonymous> (bundle.js:2541)
at __webpack_require__ (bundle.js:622)
at bundle.js:668
at bundle.js:671
我的WebPack是:
import webpack from 'webpack';
import merge from 'webpack-merge';
import path from 'path';
import isDev from 'isdev';
import { Dir } from './src/utils';
const TARGET = process.env.npm_lifecycle_event;
let Config = {
entry: [
'babel-polyfill',
'react-hot-loader/patch',
path.join(Dir.src, 'client.js'),
],
output: {
path: path.join(Dir.public, 'build'),
filename: 'bundle.js',
},
target: 'node',
resolve: {
modules: [Dir.src, 'node_modules'],
extensions: ['*', '.js', '.jsx', '.json'],
},
module: {
rules: [
{
test: /\.js?$/,
enforce: 'pre',
loader: 'eslint-loader',
exclude: /node_modules/,
include: Dir.src,
},
{
test: /\.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
],
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
},
}),
],
};
if (TARGET === 'build:prod' && !isDev) {
Config = merge(Config, {
bail: true,
devtool: 'source-map',
output: { publicPath: '/build/' },
plugins: [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
comments: false,
dropDebugger: true,
dropConsole: true,
compressor: {
warnings: false,
},
}),
],
});
}
if (TARGET === 'server:dev' && isDev) {
Config = merge(Config, {
devtool: 'eval',
entry: ['webpack-hot-middleware/client'],
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
],
});
}
const WebpackConfig = Config;
export default WebpackConfig;
此錯誤纔開始顯示出來,一旦我說什麼終極版提出了服務器端渲染。因此,我正在使用窗口.__ PRELOADED_STATE__在./src/utils/store.js中的商店水合,並且它也在index.ejs這是呈現給客戶端的文件。
這也是我.babelrc如果有的話:
{
"presets": ["es2015", "react", "stage-0"],
"env": {
"development": {
"plugins": ["react-hot-loader/babel"],
},
},
"plugins": [
"babel-root-import"
],
}
希望任何人都可以在這方面幫助 - 我沒有找到我的研究和試驗的解決方案。謝謝!
感謝那@Stefan - 你是對的。我說的唯一原因是因爲我有一個問題說'無法解析'fs'',並且提出的解決方案是'target:'node'':https://github.com/webpack-contrib/css-loader/問題/ 447 ...然後我得到了'全局未定義',所以我有一種錯誤循環,並且找不到任何東西來殺死所有它們...任何想法? – adambargh
從邏輯上講,你需要獲得webpack來模擬彈出的所有東西。例如'fs':'node:{fs:'empty'}'。我不知道這是否是你想要做的正確解決方案。 –
也就是'webpack.config.js'中的'node:{global:true,fs:'empty'}' –