简体   繁体   English

Webpack,React.js-在浏览器控制台中要求未定义的错误

[英]Webpack, React.js - require not defined error in browser console

I'm trying to build a web app with a React.js front-end, Express handling the back-end and Webpack bundling the whole thing. 我正在尝试用React.js前端构建一个Web应用程序,Express处理后端,Webpack将整个事情捆绑在一起。 I'm trying to move away from my habitual way of doing things which is creating separate webpack.config files for the server and client. 我试图摆脱我惯常的做事方式,即为服务器和客户端创建单独的webpack.config文件。 I'm also trying to add a minifier (Babili). 我还试图添加一个缩小器(Babili)。

Here is my webpack.config file. 这是我的webpack.config文件。 Note how I used object.assign to create different objects for my client and server files and how I export them at the very end. 请注意如何使用object.assign为客户端和服务器文件创建不同的对象,以及最终如何导出它们。 I doubt this is where the problem lies. 我怀疑这就是问题所在。

const BabiliPlugin = require('babili-webpack-plugin');
const nodeExternals = require('webpack-node-externals');
const path = require('path');
const srcPath = path.resolve(__dirname + '/src');
const distPath = path.resolve(__dirname + '/dist');

// Common entries for all configs
var common = Object.assign({}, {
  context: srcPath,
  resolve: {
    modules: ['node_modules', 'src'],
    extensions: ['*']
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  },
  plugins: [
    new BabiliPlugin()
  ],
  externals: nodeExternals()
});

// Server.js config
// Output to dist/client/
var serverConfig = Object.assign({}, common, {
  entry: './server/index.js',
  output: {
    path: distPath,
    filename: 'server.min.js'
  },
  target: 'node',
  node: {
    __dirname: false,
    __filename: false
  }
});

// Client.js config
// Output to /dist/
var clientConfig = Object.assign({}, common, {
  entry: "./client/index.js",
  output: {
    path: distPath,
    filename: './client/client.min.js',
    publicPath: '/'
  },
  target: 'web',
  devtool: 'source-map'
});

// Export configurations array
module.exports = [serverConfig, clientConfig]

Here is my client.js file: 这是我的client.js文件:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route } from 'react-router-dom';

import Home from './routes/Home.js';

ReactDOM.render((
  <div>
    <p> why is this not working </p>
  </div>
), document.getElementById('app'));

The error I get in the browser console is the following : 我在浏览器控制台中遇到的错误如下:

Uncaught ReferenceError: require is not defined
at Object.<anonymous> (client.min.js:1)
at b (client.min.js:1)
at Object.<anonymous> (client.min.js:1)
at b (client.min.js:1)
at client.min.js:1
at client.min.js:1

I don't understand why it wouldn't work. 我不明白为什么它行不通。 The server.js file works fine since I can see the index.html file is served to the browser. server.js文件运行正常,因为我可以看到index.html文件已提供给浏览器。 My usual webpack.config files are the exact same except for the Babili minifier, which when removed does not solve the issue. 除Babili缩小器外,我通常的webpack.config文件完全相同,删除时不能解决问题。 I'm hoping you guys can help me out with this. 我希望你们能帮我这个忙。 Thank you in advance! 先感谢您!

Edit: I'd like to add the fact I did not have the nodeExternals() part in my previous client config. 编辑:我想添加一个事实,我之前的客户端配置中没有nodeExternals()部分。 However, when I don't include it, I get the following error: 但是,当我不包括它时,会出现以下错误:

Uncaught Error: Cannot find module "object-assign"
at client.min.js:8
at client.min.js:8
at Object.<anonymous> (client.min.js:8)
at Object.<anonymous> (client.min.js:8)
at t (client.min.js:1)
at Object.<anonymous> (client.min.js:1)
at Object.<anonymous> (client.min.js:1)
at t (client.min.js:1)
at Object.<anonymous> (client.min.js:1)
at t (client.min.js:1)

externals: nodeExternals () tells Webpack to load all modules using require . externals: nodeExternals ()告诉Webpack使用require加载所有模块。 This is useful for the server but throws this error in the browser (because require is only natively present on Node.js). 这对服务器很有用,但会在浏览器中引发此错误(因为require仅在Node.js上本地存在)。

To fix it simply move the externals field to the server config : 要解决此问题,只需将externals字段移动到服务器配置:

// Common entries for all configs
var common = Object.assign({}, {
  context: srcPath,
  resolve: {
    modules: ['node_modules', 'src'],
    extensions: ['*']
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  },
  plugins: [
    new BabiliPlugin()
  ]
});

// Server.js config
// Output to dist/client/
var serverConfig = Object.assign({}, common, {
  entry: './server/index.js',
  output: {
    path: distPath,
    filename: 'server.min.js'
  },
  target: 'node',
  node: {
    __dirname: false,
    __filename: false
  },
  externals: nodeExternals()
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM