webpack - Electron Forge 与 Webpack 在导入 "require is not defined"时获取 'events'

标签 webpack electron electron-forge

我最近开始了一个使用 Electron Forge 和 webpack 模板的新项目。我正在创建一个简单的身份验证类,并希望在 Node.js 中使用事件系统。我正在使用导入语法,但收到错误消息,指出 require 未定义。我不想打开 nodeIntegration 并且我认为 webpack 会自动使用 browserfy 版本的 nodejs 事件系统:

You usually do not have to install events yourself! If your code runs in Node.js, events is built in. If your code runs in the browser, bundlers like browserify or webpack also include the events module.

安装 browserify 事件模块也无法解决该问题。我还尝试排除 node_modules/events 文件夹,希望 webpack 使用自己的事件模块,但这对我不起作用。这是我的代码:

import { EventEmitter } from 'events';

export default class Auth {
  constructor(socket) {
  }

  setToken(token) {
    this.token = token;
    localStorage.setItem('token', token);
    this.emit('token', token);
  }

  checkToken() {
    if(localStorage.getItem("token") !== null) {
      this.setToken(localStorage.getItem("token"));
    } else {
      this.emit('false', false);
    }
  }

  getToken() {
    return localStorage.getItem("token");
  }

  setAuth(auth) {
    this.emit('auth', auth);
    this.isAuth = auth;
  }
  
}

这是我的 webpack 渲染器配置:

const rules = require('./webpack.rules');
const webpack = require('webpack');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');

rules.push({
  test: /\.css$/,
  use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
});

const assets = [ 'assets' ];
const copyPlugins = assets.map((asset) => {
  return new CopyWebpackPlugin({
    patterns: [{ from: path.resolve(__dirname, 'src/renderer/', asset), to: asset }],
  });
});

module.exports = {
  // Put your normal webpack config below here
  module: {
    rules,
  },
  plugins: [
      new webpack.ProvidePlugin({
          $: 'jquery',
      }),
      ...copyPlugins
  ],
  resolve: {
    alias: {
      'components': path.resolve(__dirname, './src/renderer/components'),
      'pages': path.resolve(__dirname, './src/renderer/pages'),
      'core': path.resolve(__dirname, './src/renderer/core'),
    },
    extensions: ['.js']
  },
};

这是我的 webpack 规则配置:

module.exports = [
  // Add support for native node modules
  {
    test: /\.node$/,
    use: 'node-loader',
  },
  {
    test: /\.(m?js|node)$/,
    parser: { amd: false },
    use: {
      loader: '@marshallofsound/webpack-asset-relocator-loader',
      options: {
        outputAssetBase: 'native_modules',
      },
    },
  },
];

这是错误:

external "events":
Uncaught ReferenceError: require is not defined
    at Object.events (external "events":1)
    at __webpack_require__ (bootstrap:832)
    at fn (bootstrap:129)
    at Module../src/renderer/core/Auth.js (Auth.js:1)
    at __webpack_require__ (bootstrap:832)
    at fn (bootstrap:129)
    at Module../src/renderer/index.js (index.js:1)
    at __webpack_require__ (bootstrap:832)
    at fn (bootstrap:129)
    at Object.0 (routes.js:40)

最佳答案

休息一下后,我意识到解决方案很简单:

我只需要将以下内容添加到我的渲染器配置中:

target: 'web'

关于webpack - Electron Forge 与 Webpack 在导入 "require is not defined"时获取 'events',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66112475/

相关文章:

javascript - 我无法理解 "Module not found: Error: Can' t 解析 'fs' '"

javascript - 在 webpack 和 es6 中使用 LESS

javascript - 带有 ReactJS 的 ASP.NET 6 - 使用 react-scripts 5.0.0 热重载时浏览器不刷新

node.js - 如何从自定义镜像安装Electron

electron - 目标 `GitLab CI`上的 zipper 时, Electron 伪造制造商失败

webpack.config.js 不断将bundle.js 的脚本标记添加到我的index.html 中

javascript - Electron 无法加载具有完整功能的 Web 应用程序

angular - 可执行文件太大

reactjs - Electron Forge、React 等构建已经疯狂

javascript - ElectronJS npm start/bash : electron-forge: command not found