Electron/Tedious:未定义要求("events")

Electron/Tedious: require("events") is not defined

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

重现步骤:

然后将以下行添加到 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 -> 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 和其他设置,但没有任何结果。有什么想法吗?

我想通了。

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

更改后,要求引用错误消失了。