【问题标题】:Electron/Tedious: require("events") is not defined电子/乏味:需要(“事件”)未定义
【发布时间】:2021-02-18 17:27:16
【问题描述】:

我对乏味/webpack/node/electron 有疑问?我不确定。

重现步骤:

  • git clone https://github.com/codesbiome/electron-react-webpack-typescript-2020
  • yarn install
  • yarn add tedious
  • yarn add @types/tedious

然后将以下行添加到App.tsx(或其他任何地方,仅此而已):

    const connectToDb = (): Promise<Connection> => {
      return new Promise((resolve, reject) => {
        const config = { // those data doesn't matter much
          authentication: {
            options: {
              userName: "sa", // update me
              password: "" // update me
            },
            type: "default"
          },
          server: "test_db@localhost", // update me
          options: {
            database: "test", //update me
            encrypt: true
          }
        };
      
        const connection = new Connection(config) // error is caused by this
        connection.connect()
      })
    }
    
    connectToDb().then(res => {
      console.log('res: ', res);
    }).catch(err => {
      console.log('err: ', err);
    })

控制台出现以下错误 (View -&gt; Toggle Developer Tools)

Uncaught ReferenceError: require is not defined
    at Object.events (index.js:140037)
    at __webpack_require__ (index.js:790)
    at fn (index.js:101)
    at Object../node_modules/tedious/lib/bulk-load.js (index.js:99091)
    at __webpack_require__ (index.js:790)
    at fn (index.js:101)
    at Object../node_modules/tedious/lib/tedious.js (index.js:110052)
    at __webpack_require__ (index.js:790)
    at fn (index.js:101)
    at Module.<anonymous> (index.js:139704)

单击堆栈跟踪中的第一个重定向,在此module.exports = require("events") 中显示错误,这正是问题所在的require

我尝试稍微操纵webpack 设置也尝试启用nodeIntegration 和其他设置,但没有任何结果。有什么想法吗?

【问题讨论】:

    标签: typescript webpack electron tedious


    【解决方案1】:

    我想通了。

    修复: main.tsx -> BrowserWindow -> webPreferences 并将 contextIsolation 更改为 false 并将 nodeIntegration 更改为 true

    修改后,require reference 错误消失了。

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 2017-12-28
      • 2020-04-26
      • 2019-10-11
      • 2020-10-22
      • 2020-01-27
      相关资源
      最近更新 更多