typescriptwebpackelectrontedious

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


I have a problem with tedious/webpack/node/electron? I'm not sure.

Steps to reproduce:

then add following lines to App.tsx (or anywhere else, just all it):

    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);
    })

and following error occurs in 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)

Clicking on first redirect from stacktrace showing error in this module.exports = require("events"), this is exactly the require that's the problem.

I tried to manipulate webpack settings a bit also tried enabling nodeIntegration and others, but without any result. Any ideas?


Solution

  • EDIT 2023.

    Due to security reasons this answer is no longer a good approach.

    Take a look at: Electron require() is not defined

    Original answer

    I figured it out.

    Fix: main.tsx -> BrowserWindow -> webPreferences and change contextIsolation to false and also nodeIntegration to true.

    After changing it, require reference error gone.