Electron / Tedious: требование («события») не определено

#typescript #webpack #electron #tedious

#машинописный текст #webpack #электрон #утомительный #typescript #electron

Вопрос:

У меня проблема с tedious / 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);
    })
  

и в console () возникает следующая ошибка 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)

  

Нажав на первое перенаправление из stacktrace, показывающее ошибку в этом module.exports = require("events") , это именно require то, в чем проблема.

Я попытался немного манипулировать webpack настройками, также попробовал включить nodeIntegration и другие, но без какого-либо результата. Есть идеи?

Ответ №1:

Я все понял.

Исправьте: main.tsx -> BrowserWindow -> webPreferences и измените contextIsolation на false , а также nodeIntegration на true .

После его изменения ошибка require reference исчезла.