ReferenceError:未在eval中定义global

ada*_*rgh 0 babel webpack redux 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
Run Code Online (Sandbox Code Playgroud)

我的网站是:

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;
Run Code Online (Sandbox Code Playgroud)

一旦我添加了Redux为服务器端呈现建议的内容,此错误才开始显示.所以我在./src/utils/store.js中使用带有窗口.__ PRELOADED_STATE__的存储的水合作用,它也在index.ejs中,它是呈现给客户端的文件.

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

{
    "presets": ["es2015", "react", "stage-0"],
    "env": {
        "development": {
            "plugins": ["react-hot-loader/babel"],
        },
    },
    "plugins": [
        "babel-root-import"
    ],
}
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*nev 7

我认为,问题target: 'node'在于你的webpack.config.js.这基本上指出的WebPack可以假设该集合将在节点状的环境中,其中,相同的全局运行globalrequire由环境提供的.除非另有说明,否则Webpack假定浏览器环境并重写global为指向window.您的配置禁用此重写.

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

  • 也就是说,`webpack.config.js` 中的 `node: {global: true, fs: 'empty'}` (2认同)