webpack - 引用错误 : global is not defined at eval

标签 webpack redux babeljs react-hot-loader babel-polyfill

我遇到了一个我认为是来自 webpack 的错误。在这里:

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;

只有在我添加了 Redux 建议的服务器端渲染后,这个错误才开始出现。所以我在 ./src/utils/store.js 中使用 window.__PRELOADED_STATE__ 的存储水合作用,它也在 index.ejs 中,这是呈现给客户。

如果有的话,这也是我的 .babelrc:

{
    "presets": ["es2015", "react", "stage-0"],
    "env": {
        "development": {
            "plugins": ["react-hot-loader/babel"],
        },
    },
    "plugins": [
        "babel-root-import"
    ],
}

希望任何人都可以帮助解决这个问题——我在研究和试验中还没有找到解决方案。谢谢!

最佳答案

问题是,我认为,这个 target: 'node' 在你的 webpack.config.js 中。这基本上说明了 Webpack 可能假设 bundle 将在类似节点的环境中运行,其中 globalrequire 等全局变量由环境提供。除非另有说明,否则 Webpack 假定浏览器环境并重写 global 以指向 window。您的配置禁用此重写。

您可以从配置中删除 target: 'node',或者通过添加 node: {global: true} 显式启用 global 重写> 到您的配置对象。

关于webpack - 引用错误 : global is not defined at eval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45175521/

相关文章:

javascript - 如何完全禁用 babel transform-regenerator

WebPack加载器配置: To exclude or to include node_modules?

JQuery HoverIntent 无法与 Webpack 和 ProvidePlugin 一起使用

typescript - 找不到模块 : Error: Cannot resolve module '@types/lodash'

typescript - 如何为 umd 库创建 TypeScript 定义文件 (d.ts)

reactjs - 错误: Could not find router reducer in state tree,它必须安装在 "router"下

reactjs - 使用 Jest 和 Enzyme 测试使用 Redux 的功能 React 组件

javascript - Redux:API 数据响应按点击排序

node.js - webpack +express.js 不加载bundle.js

javascript - Karma 测试 React 与 ES6/Babel