question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't install the service worker in Electron renderers with `nodeIntegration` enabled

See original GitHub issue

Describe the bug

Invoking worker.start() in an Electron renderer fails if nodeIntegration is enabled. This happens because process is defined, which causes isNodeProcess to return true. As a result, it’s not possible to use msw in these windows. (msw’s node server doesn’t work in that environment either, since fetch calls use the Chromium network stack, not Node’s.)

Electron provides process.type with value "renderer" in these situations. Would it be satisfactory for isNodeProcess to return false in that case? If so, I’m happy to put up a PR!

Environment

  • msw: 0.27.1
  • nodejs: 12.18.3
  • npm: 7.6.3

Please also provide your browser version.

Electron 11.3.0 / Chromium 87

To Reproduce

Steps to reproduce the behavior:

  1. Create Electron BrowserWindow with nodeIntegration: true
  2. Create an msw service worker and invoke worker.start()
  3. Open DevTools, view Console
  4. See error:
[MSW] Failed to execute `setupWorker` in a non-browser environment. Consider using `setupServer` for Node.js environment instead.

Expected behavior

Service worker is installed

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Akallabetcommented, Aug 6, 2022

@kettanaito it looks like the new lib is-node-process, which msw relies upon, doesn’t have this change

if (process?.type === 'renderer') {
  return true
}

If I understood correctly, this was the accepted fix?

1reaction
Pagebakerscommented, May 20, 2022

I think there has been a regression. I’m not able to run msw in Electron anymore.

Unhandled Runtime Error
Error: [MSW] Failed to execute `setupWorker` in a non-browser environment. Consider using `setupServer` for Node.js environment instead.
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I register a Service Worker in Electron when node ...
I have finally figured out how to use a service worker if node integration is enabled for both the renderer thread as well...
Read more >
最新趨勢觀測站- electron service worker的推薦與評價
electron service worker 在Can't install the service worker in Electron renderers . ... in an Electron renderer fails if nodeIntegration is enabled.
Read more >
Electron release notes
Added support for Webauthn discoverable keys (aka resident keys), allowing users to authenticate without first having to select or type a username.
Read more >
Quick Start with Electron using React — Realm
This page contains information to get Realm integrated into your Electron application quickly by developing an application using Create React App.
Read more >
Electron Node Integration - Quasar v1
js resources from within the “renderer” thread (the UI). It is enabled by default in Quasar CLI, although Electron is encouraging developers to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found