When it comes to installation and configuration the official documentation states the following:
All you have to do now is
import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer'; installExtension(REACT_DEVELOPER_TOOLS) .then((name) => console.log(`Added Extension: ${name}`)) .catch((err) => console.log('An error occurred: ', err));
That is a bit concise.
To open DevTools, use the shortcut "Ctrl+Shift+I" or the <F12> key. You can check out how to use devtools here.
To load an extension in Electron, you need to download it via Chrome, locate its filesystem path, and then load it into your Session by calling the [ ses. loadExtension ] API. Using the React Developer Tools as an example: Install the extension in Google Chrome.
Disable developer tools in productionThe key-combination CTRL + SHIFT + I (or ALT + CMD + I on Mac) will open the dev tools and enable inspection of the application. It will even enable some degree of modification.
Place it in the file where you include Electron and run it after the ready
event was emitted by it:
const { app } = require('electron');
app.on('ready', functionWithTheCodeFromDocs);
You only need to do this once! The extensions will persist after this code have been run.
Install the package just like the docs instruct you. In case of npm:
npm install electron-devtools-installer --save-dev
You may require the package and configure it in the file where you build up your Electron app. You need to include the installer function and the (possibly multiple) needed extension(s):
With ES6 modules:
import installExtension, { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } from 'electron-devtools-installer';
With require
:
const { default: installExtension, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } = require('electron-devtools-installer');
I will presume that somewhere you required Electron:
const { app } = require('electron');
The installExtension
function have to be called after the ready
event was emitted by the application. If you want to add multiple extensions you have to call the function multiple times with the different extensions by copy-paste:
app.on('ready', () => {
installExtension(REACT_DEVELOPER_TOOLS)
.then((name) => console.log(`Added Extension: ${name}`))
.catch((err) => console.log('An error occurred: ', err));
});
app.on('ready', () => {
installExtension(REDUX_DEVTOOLS)
.then((name) => console.log(`Added Extension: ${name}`))
.catch((err) => console.log('An error occurred: ', err));
});
Or you can write a loop:
app.on('ready', () => {
[REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS].forEach(extension => {
installExtension(extension)
.then((name) => console.log(`Added Extension: ${name}`))
.catch((err) => console.log('An error occurred: ', err));
});
});
If you have done everything properly, after you run electron .
in your project's folder, you should see this in the console:
Added Extension: React Developer Tools
Added Extension: Redux DevTools
Keep in mind: You only need to run this code once. As Electron's BrowserWindow.addDevToolsExtension
documentation states:
The extension will be remembered so you only need to call this API once, this API is not for programming use. If you try to add an extension that has already been loaded, this method will not return and instead log a warning to the console.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With