bugsnag/bugsnag-js

Bugsnag for electron ReferenceError: self is not defined (Vite)

rgutt opened this issue · 2 comments

rgutt commented

Describe the bug

I'm writing a cross platform app using electron and vue. Everything is writting using typescript and vite is used as a bundler for the main electron code and the renderer. Without bundling the electron code everything works fine but when bundling the code I get the following error:

App threw an error during load ReferenceError: self is not defined at requireFingerprint_browser (...\.vite\build\main.js:151:10809) at requireCuid (..\vite\build\main.js:151:11166) at requireSession$2 (...\.vite\build\main.js:151:11623) at requireClient (..\\.vite\build\main.js:151:13404) at requireMain (..\\.vite\build\main.js:163:4634) at Object.<anonymous> (..\\.vite\build\main.js:163:19296) at Module._compile (node:internal/modules/cjs/loader:1174:14) at Module._extensions..js (node:internal/modules/cjs/loader:1229:10) at Module.load (node:internal/modules/cjs/loader:1044:32) at Module._load (node:internal/modules/cjs/loader:885:12)

When removing all bugsnag imports the bundling works flawless.

Steps to reproduce

  1. Run npm init electron-app@latest my-new-app -- --template=vite
  2. Run npm install --save @bugsnag/electron
  3. Add the following lines to main.js right at the beginning:
    import Bugsnag from "@bugsnag/electron"; Bugsnag.start({});
  4. Run npm run start
  5. Stop if its running
  6. Add in package.json the following script entry: "start-electron": "electron .vite\\build\\main.js"
  7. Run npm run start-electron
  8. Get the same error.

Environment

  • Bugsnag version: 7.20
  • Browser framework version (if any):
    • Vue: /
    • React: /
    • Angular: /
  • Server framework version (if any):
    • Koa: /
    • Express: /
    • Restify: /
  • Browser version (e.g. chrome, safari): Electron 24.0.0
  • Device (e.g. iphonex): Windows Laptop (10)
rgutt commented

I solved it. The probelm was that vite is by default using the "browser" option in the package.json. So it was importing the wrong fingerprint function.

"browser": { "./lib/fingerprint.js": "./lib/fingerprint.browser.js" },

By setting browserField: false in vite.config.js it will ignore the browser option and the error is gone. Easy fix. Took me a while to figure it out...

rgutt commented

Can be closed.