How to Set Up an Electron.js Project with React, Vite, Tailwind CSS, and TypeScript
Introduction
In this guide, you’ll learn how to set up a modern Electron.js project using React, Vite, Tailwind CSS, and TypeScript. We’ll walk through each step to create a well-structured project that leverages the best tools for building desktop applications with a web technology stack.
Prerequisites
Before we start, make sure you have the following installed on your machine:
- Node.js: You can download it from nodejs.org.
- npm or yarn: These come with Node.js, so you should already have them.
Step 1: Create the Project Folder Structure
First, create a new directory for your project. Inside this directory, we’ll have two main folders: one for the Electron setup and another for the frontend (React) setup.
- Open your terminal and navigate to the location where you want to create your project.
- Run the following commands:bashCopy code
mkdir ibex-simulator cd ibex-simulator mkdir frontend
Step 2: Set Up the React Project with Vite
Next, we’ll create the React app inside the frontend
directory using Vite.
- Navigate to the
frontend
directory:bashCopy codecd frontend
- Create a new Vite project:bashCopy code
npm create vite@latest
- Follow the prompts to set up your project:
- Project name:
frontend
- Select a framework:
React
- Select a variant:
TypeScript
- Project name:
- Install the dependencies:bashCopy code
npm install
Step 3: Add Tailwind CSS
Now, let’s add Tailwind CSS to style your React components.
- Install Tailwind CSS:bashCopy code
npm install -D tailwindcss postcss autoprefixer
- Initialize Tailwind CSS:bashCopy code
npx tailwindcss init -p
- Configure Tailwind by editing the
tailwind.config.js
file:javascriptCopy code/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./index.html", "./src/**/*.{js,ts,jsx,tsx}", ], theme: { extend: {}, }, plugins: [], }
- Add Tailwind directives to your CSS file:In
src/index.css
, replace the content with:cssCopy code@tailwind base; @tailwind components; @tailwind utilities;
Step 4: Set Up Electron in the Root Directory
Now, let’s set up Electron in the root of your project.
- Navigate back to the root directory:bashCopy code
cd ..
- Initialize a new Node.js project:bashCopy code
npm init -y
- Install Electron and development tools:bashCopy code
npm install --save-dev electron concurrently cross-env electron-builder
Step 5: Create main.js
for Electron
Create a main.js
file in the root directory. This file will manage the Electron window.
- Create
main.js
:bashCopy codetouch main.js
- Add the following code to
main.js
:javascriptCopy codeconst { app, BrowserWindow } = require('electron'); const path = require('path'); function createWindow() { const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { preload: path.join(__dirname, 'preload.js'), }, }); if (process.env.NODE_ENV === 'development') { mainWindow.loadURL('http://localhost:5173'); } else { mainWindow.loadFile(path.join(__dirname, 'dist/index.html')); } } app.on('ready', createWindow); app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } });
Step 6: Modify package.json
Scripts
Update the package.json
in the root directory to include scripts for running and building the project.
- Open the
package.json
file and update the scripts section:jsonCopy code"scripts": { "start": "electron .", "dev": "concurrently \"npm run dev-frontend\" \"cross-env NODE_ENV=development electron .\"", "dev-frontend": "npm run dev --prefix frontend", "build": "npm run build-frontend && electron-builder", "build-frontend": "npm run build --prefix frontend" }
Step 7: Run the Project
Now, let’s run the project to see everything in action.
- From the root directory, run:bashCopy code
npm run dev
- This will:
- Start the Vite development server in the
frontend
directory. - Launch Electron and load your React app in an Electron window.
- Start the Vite development server in the
Step 8: Create and Display a Simple React Component
Here’s an example of a basic React component.
- In
frontend/src/App.tsx
, replace the content with:typescriptCopy codeimport React from 'react'; import './App.css'; function App() { return ( <> <h1 className="text-2xl font-bold text-center text-blue-600">Khan Sab</h1> </> ); } export default App;
- Tailwind CSS styles are applied to make the text bold, centered, and blue.
Conclusion
Congratulations! You’ve successfully set up an Electron.js project with React, Vite, Tailwind CSS, and TypeScript. This setup gives you a powerful environment for building cross-platform desktop applications with modern web technologies.