#Table of Contents
#Prerequisites
Before diving into this blog, ensure you have a solid understanding of the following topics:
- Web Development Fundamentals
- React Basics: Familiarity with the basic structure of a React project.
- TypeScript (optional): While not mandatory, it can be helpful.
#What is Electron?
Electron is a framework for building desktop applications using web technologies like HTML, CSS, and JavaScript.
It enables the creation of cross-platform apps that work seamlessly(not everytime 😐) on Windows, macOS, and Linux.
In essence, you are writing simple web code (HTML CSS and JS) to build powerful(but quite heavy 😅) desktop applications.
Must know: A simple "Hello World" program in Electron can take up around 180MB of disk space.
My Advice: For a deeper understanding, refer to the official Electron Documentation.
#Why Electron?
Even with its large size compared to traditional methods, Electron stands out because:
- It's easy to use, making it accessible for web developers.
- It has a supportive and active community that continuously improves the framework.
- The learning curve is minimal, as it's straightforward and beginner-friendly.
#Setting Up the Project (React)
- Create a vite project
shnpm create vite@latest
Do the following :
- Choose the project name you want
- Select React as framework
- Select TypeScript (best practices)
and then run :
shcd your-project-name npm install
Optional: Initialize a Git repository to track your changes and collaborate effectively if needed.
Try Running the react app you would see:
-
Create a new folder named
web
inside thesrc
directory. This folder will house the web-specific part of your project. -
Copy all the files from the
src
directory into theweb
folder. This separation ensures that the web and Electron parts of the project remain organized and modular. -
Update the
<script>
tag'ssrc
attribute in yourindex.html
file to point to the correct path.
This ensures that your application loads the appropriate scripts for the Electron environment.
html<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="icon" type="image/svg+xml" href="/vite.svg" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Vite + React + TS</title> </head> <body> <div id="root"></div> <script type="module" src="/src/web/main.tsx"></script> </body> </html>
Now run your react app via npm run dev
and it will work as intended :)
#Adding Electron
#Step 1: Create a Folder for Electron
Create a folder named electron
inside the src
directory.
This folder will contain all the Electron-specific code, keeping it separate from the web code.
#Step 2: Install Electron
Install Electron as a dev dependency by running the following command:
shnpm install electron --save-dev
- Create a folder named
electron
inside thesrc
directory. - Install Electron
shnpm install electron --save-dev
- Create a
tsconfig.json
insidesrc
as we will have differenttypescript
setup for electron app.
json// src/electron/tsconfig.json { "compilerOptions": { "strict": true, "target": "ESNext", "module": "NodeNext", "outDir": "../../dist-electron", "skipLibCheck": true, "types": ["../../types"] } }
- In the
tsconfig.app.json
excludesrc/electron
json// tsconfig.app.json { "compilerOptions": { "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", "target": "ES2020", "useDefineForClassFields": true, "lib": ["ES2020", "DOM", "DOM.Iterable"], "module": "ESNext", "skipLibCheck": true, /* Bundler mode */ "moduleResolution": "bundler", "allowImportingTsExtensions": true, "isolatedModules": true, "moduleDetection": "force", "noEmit": true, "jsx": "react-jsx", /* Linting */ "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, "noUncheckedSideEffectImports": true }, "include": ["src"], "exclude": ["src/electron"] }
- Create
main.ts
inside theelectron
folder.Write any console you like for example
tsxconsole.log("This is the_demon_sid from electron")
- In
package.json
add
json"main" : "dist/electron/main.js" ... "scripts":{ ... "dev:electron" : "electron ." }
this will run our main.js in dist/electron folder with elctron and you will receive a log on terminal.