electron / electron

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS
https://electronjs.org
MIT License
112.73k stars 15.11k forks source link

[Bug]: React devtools doesn't attach to devtools on launch, reload needed #41613

Open SantiagoChiappe opened 3 months ago

SantiagoChiappe commented 3 months ago

Preflight Checklist

Electron Version

29.1.4

What operating system are you using?

Windows

Operating System Version

Windows 11 Home 23H2

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

When I launch my Electron app on developer mode, React Devtools are installed and attached to my app.

Actual Behavior

React Devtools are installed, but not attached to my app. A manual reload or force reload is required to attach it. Same problem if I installed React Devtools manually from Chrome extensions, have to reload manually.

bug01 bug02

bug03

bug04

Testcase Gist URL

No response

Additional Information

My code :

// Import required modules
const { default: installExtension, REACT_DEVELOPER_TOOLS } = require('electron-devtools-installer');
const path = require('path');
const { app, BrowserWindow } = require('electron');

// Initialize mainWindow variable
let mainWindow;

// Function to create the main window
const createWindow = async () => {
  const isDev = await import('electron-is-dev');

  // Configure the main window
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true, // Enable Node.js integration
      enableRemoteModule: true, // Enable remote module
      contextIsolation: false, // Disable context isolation
      autoHideMenuBar: true, // Auto-hide menu bar
      devTools: isDev.default, // Show DevTools in development
    },
  });

  // Hide the menu bar
  mainWindow.setMenuBarVisibility(true);

  // Load the appropriate URL based on the environment
  mainWindow.loadURL(
    isDev.default
      ? 'http://localhost:3000' // Development URL
      : `file://${path.join(__dirname, '../build/index.html')}` // Production URL
  );
};

// Create the main window when the app is ready
app.whenReady().then(() => {
  installExtension(REACT_DEVELOPER_TOOLS,  { loadExtensionOptions: { allowFileAccess: true }})
      .then((name) => {
        console.log(`Added Extension: ${name}`);
        // Create the window and the React application
        createWindow();
      })
      .catch((err) => console.log('An error occurred: ', err));
});

// Quit the app when all windows are closed (except on macOS)
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

// Create a new window when the app is activated (macOS)
app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

I tried also reloading the window on my code or adding a timer to reload the window but without success.

erikian commented 2 months ago

@SantiagoChiappe are you getting an error similar to this (reported in https://github.com/electron/electron/issues/37876#issuecomment-1984873828) in your main process?

[91044:0308/100700.690484:ERROR:CONSOLE(2)] "Electron sandboxed_renderer.bundle.js script failed to run", source: node:electron/js2c/sandbox_bundle (2)
[91044:0308/100700.690537:ERROR:CONSOLE(2)] "TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))", source: node:electron/js2c/sandbox_bundle (2)
SantiagoChiappe commented 2 months ago

@SantiagoChiappe are you getting an error similar to this (reported in #37876 (comment)) in your main process?

[91044:0308/100700.690484:ERROR:CONSOLE(2)] "Electron sandboxed_renderer.bundle.js script failed to run", source: node:electron/js2c/sandbox_bundle (2)
[91044:0308/100700.690537:ERROR:CONSOLE(2)] "TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))", source: node:electron/js2c/sandbox_bundle (2)

Yes I do, only when opening devtools. I also got other errors when I reload the app using View -> Reload, but then react devtools appear as I mentioned previously :


[1] [73616:0410/004950.275:ERROR:CONSOLE(1)] "uniqueContextId not found", source: devtools://devtools/bundled/core/sdk/sdk.js (1)
[1] [73616:0410/004950.275:ERROR:CONSOLE(1)] "Extension server error: Inspector protocol error: uniqueContextId not found", source: devtools://devtools/bundled/models/extensions/extensions.js (1)
[1] [73616:0410/004950.278:ERROR:CONSOLE(1)] "Received error with code E_PROTOCOLERROR while checking if react has loaded: "Inspector protocol error: %s"", source: chrome-extension://deachpobkibmlfmamelbofgimdagiiob/build/main.js (1)```
louispelarrey commented 2 months ago

Got the same issue

Jurin commented 2 months ago

Exact same issue but with Vue devtools with the exception that it will not load even after reloading. Same error when trying to open devtools.

[24844:0422/101748.810:ERROR:CONSOLE(1)] "Request Autofill.enable failed. {"code":-32601,"message":"'Autofill.enable' wa
sn't found"}", source: devtools://devtools/bundled/core/protocol_client/protocol_client.js (1)
[24844:0422/101748.903:ERROR:CONSOLE(2)] "Electron renderer.bundle.js script failed to run", source: node:electron/js2c/
renderer_init (2)
[24844:0422/101748.903:ERROR:CONSOLE(2)] "TypeError: object null is not iterable (cannot read property Symbol(Symbol.ite
rator))", source: node:electron/js2c/renderer_init (2)
khanayaan98 commented 2 months ago

Any update on this? how to resolve the issue?

alexk111 commented 2 months ago

Resolved this by using the react-devtools package instead of installing the chrome extension.

  1. Exec yarn add --dev react-devtools
  2. Add "dev:react-devtools": "react-devtools" or something like that to the scripts in the package.json
  3. Add <script src="http://localhost:8097"></script> to the dev version of your renderer index html file
  4. Now when you exec yarn run dev:react-devtools it will launch the react-devtools server and make the react devtools available in the app window devtools.
lanshenghai commented 1 month ago

Resolved this by using the react-devtools package instead of installing the chrome extension.

  1. Exec yarn add --dev react-devtools
  2. Add "dev:react-devtools": "react-devtools" or something like that to the scripts in the package.json
  3. Add <script src="http://localhost:8097"></script> to the dev version of your renderer index html file
  4. Now when you exec yarn run dev:react-devtools it will launch the react-devtools server and make the react devtools available in the app window devtools.

It's workable on my project. Thanks.