This template is based on Vite React + TypeScript official template with electron dependency added. It provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
This templates also offers a preconfigured debug that works in the 'main' and 'renderer' proceses at the same time.
Also a tiped preload is configured and working.
I've never liked Electron app creation tools. They add too much boilerplate, outdated dependencies, and difficulty in understanding and manipulating the project. So I decided to experiment to find a lighter and easier way to create and manage an Electron project. One of the ways is using Vite. Vite is a powerful tool that some people use to create Electron applications. The problem is that it is oriented towards frontend and not node applications. With its use I have realized that in the long run it has limitations. So use this repo, have fun with it, learn, but if you need something really light and that works perfectly, I recommend you to use my other template electron-simple-template.
I hope you find it useful.
- Clone this Repo.
- Use
npm installto install all dependencies - Execute the
dev:electronscript to launch the app.
Note:
Depending on when you download this template, you may want to update the version of the dependencies in thepackage.json.
- The React files are stored in the
src/uifolder except theindex.htmlthat have to be in the root folder. The output files goes to thedist-reactfolder. - The Electron files are stored in the
src/electronfolder. The output files goes to thedist-electronfolder.
This template is designed to make electron work with the transpiled files stored in the dist-electron and dist-react folders.
Electron-Builder will generate distributables on the dist folder.
Nodemon is used to watch for file changes. nodemon.json contains a basic configuration.
- dev:electron: The mains script. Launch the Electron app and watch for file changes.
- dev:ui-only: Launch the Vite server with the React project. Use this if you only want to work on UI side like a webpage, but Electron queries will not work.
- watch: Executes the nodemon watch service on all files located in the
srcfolder. Every changes will make nodemon transpile all the 'src' folder. - build: Transpile the React and Electron TypeScript files to the JavaScript files in theire corresponding dist directories.
- lint: Launch the EsLint check.
- preview: Same as
dev:ui-onlybut with transpiled files. This command is usefull to check how UI will look in production. - dist:win: Electron-builder script to create Windows distributables. Output files are stored in the
distfolder. - dist:mac: Electron-builder script to create Mac distributables. Output files are stored in the
distfolder. - dist:linux: Electron-builder script to create Linux distributables. Output files are stored in the
distfolder. - clean: Delete the
dist,dist-electronanddist-reactfolders.
Modify the electron-builder.json file to generate the output you want.
Note:
Electron-builder recommends you to build the packages on the corresponding OS. What it means that you should rundist:winon Windows OS.
If you want to debug the app:
- Add the breakpoints in your code.
- run the
watchscript. - Open the VSCode debug panel and select
Electron: All"in theRUN AND DEBUGselector. and press the play button or F5.
The App will automatically launch in debug mode with the Nodemon watch activated.
When you make changes to the files, Nodemon will generate the new files. You will need to restart (or press Ctrl + Shift + F5) the debugger to display the changes.
When you use the Electron: All option, VSCode will launch 2 debugger session at the same time. So if you stay too much debugging in the main process without opening the React view, you will get an error from the second debugger. That is a normal behaviour. When you finish debuggin the main process just relaunch the debuger to continue debuging in the view.
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
- Configure the top-level
parserOptionsproperty like this:
export default tseslint.config({
languageOptions: {
// other options...
parserOptions: {
project: ["./tsconfig.node.json", "./tsconfig.app.json"],
tsconfigRootDir: import.meta.dirname
}
}
});- Replace
tseslint.configs.recommendedtotseslint.configs.recommendedTypeCheckedortseslint.configs.strictTypeChecked - Optionally add
...tseslint.configs.stylisticTypeChecked - Install eslint-plugin-react and update the config:
// eslint.config.js
import react from "eslint-plugin-react";
export default tseslint.config({
// Set the react version
settings: { react: { version: "18.3" } },
plugins: {
// Add the react plugin
react
},
rules: {
// other rules...
// Enable its recommended rules
...react.configs.recommended.rules,
...react.configs["jsx-runtime"].rules
}
});