【问题标题】:ReferenceError: global is not defined at evalReferenceError:全局未在 eval 中定义
【发布时间】:2017-12-23 20:06:05
【问题描述】:

我遇到了一个我认为来自 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"
    ],
}

希望任何人都可以提供帮助 - 我在研究和试验中还没有找到解决方案。谢谢!

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

  • 感谢@Stefan - 你是对的。我提出它的唯一原因是因为我有一个问题说 Cannot resolve 'fs' 并且提出的解决方案是 target:'node' 这里:github.com/webpack-contrib/css-loader/issues/447... 然后我得到了 global not defined 所以我有点在循环中错误并且找不到可以杀死所有这些错误的东西...有什么想法吗?
  • 症状上,你需要让 webpack 模拟所有弹出的内容。例如fsnode: { fs: 'empty' }。我不知道这是否是您尝试做的正确解决方案。
  • node: {global: true, fs: 'empty'} in webpack.config.js
  • 这样就可以解决这些错误,然后我会看到GetEnv.Nonexistent: SERVER_HOST does not exist and no fallback value provided. - 我正在用这些错误打地鼠
  • 这非常有用!!谢谢!!
猜你喜欢
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 2019-12-13
  • 2018-02-13
  • 2020-02-29
  • 2020-03-15
  • 2018-12-16
相关资源
最近更新 更多