DEV Community

Cover image for Kickstart an Electron app with React, TypeScript, Tailwind4, PostCSS and NPM
manutemu
manutemu

Posted on • Edited on

Kickstart an Electron app with React, TypeScript, Tailwind4, PostCSS and NPM

Why this post exists

Around the web, you can find all sorts of articles about creating a React app using Electron and Tailwind.
This is yet another tutorial, but it will actually work if you're planning to use the new version of Tailwind.

With the introduction of the Tailwind v4, there have been major changes to packages and the way projects are set up.

This aims to be a quick way to kickstart your project and get it up and running right away.

So, let's go.

Attention please!

For this tutorial, I used Node.js version 22, which is currently in LTS.
I tried version 23, but ran into several issues that quickly convinced me to drop that path.

Make sure you're using version 22 or install it:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
\. "$HOME/.nvm/nvm.sh"
nvm install 22
node -v # Should print "v22.14.0".

# just to be sure
nvm current # Should print "v22.14.0".
nvm use node 22
Enter fullscreen mode Exit fullscreen mode

Usual suspect: Electron app

Here we install Electron, and React stuffs.

# like most of the tutorials we install Electron
npm init electron-app@latest manutemu-sample-app -- --template=webpack-typescript
cd manutemu-sample-app
# installing react with typescript
npm install --save react react-dom
npm install --save-dev @types/react @types/react-dom
Enter fullscreen mode Exit fullscreen mode

Let’s get our hands dirty with some dirt and mud.

Create src/app.tsx file, the entry point of our react app, with this content:

import './index.css'; 
import * as React from "react";
import { createRoot } from "react-dom/client";

const root = createRoot(document.getElementById('root') as HTMLElement);
root.render(
  <React.StrictMode>
   <h1>Hello react</h1>
  </React.StrictMode>
);
Enter fullscreen mode Exit fullscreen mode

In src/renderer.ts add a reference to your app.tsx adding the following line:

import './index.css'; // this should be already here
import './app'        // add this one!
Enter fullscreen mode Exit fullscreen mode

Modify the <body> of your src/index.html like this:

<body>
    <div id="root"></div>
</body>
Enter fullscreen mode Exit fullscreen mode

In tsconfig.json add this line in compilerOptions section:

  "jsx": "react-jsx",// add this one!
Enter fullscreen mode Exit fullscreen mode

Special agent Tailwind4, we got a job for you!

Now comes the most important part of the whole article — the part that actually pushed me to write it.
It’s just a few lines, sure, but they’re crucial.
I spent time testing different setups so you don’t have to.

# installing tailwind post-css and some other thing to get the app working with Electron
npm install tailwindcss @tailwindcss/postcss postcss postcss-loader
Enter fullscreen mode Exit fullscreen mode

In webpack.renderer.config.ts add a line for postcss-loader

rules.push({
  test: /\.css$/,
  use: [
    { loader: 'style-loader' },   // should be already here
    { loader: 'css-loader' },     // should be already here
    { loader: 'postcss-loader' }  // add this one! 
  ]
})
Enter fullscreen mode Exit fullscreen mode

Create a postcss.config.js with content like this:

export default {
    plugins: {
        "@tailwindcss/postcss": {},
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, add a reference of monsieur Tailwind into your CSS.

Here, i added it on the first line on my src/index.css to use it everywhere on my app. Also, i commented some rules, but is up to you.

Here the file changed:

@import "tailwindcss";

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,
    Arial, sans-serif;
  margin: auto;
  /* max-width: 38rem; */
  /* padding: 2rem; */
}

Enter fullscreen mode Exit fullscreen mode

Then, to check whether Tailwind is working, i added some elements on the app.

I changed the whole content of src/app.tsx to prepare some sample layout.

import './index.css'; // import css
import * as React from "react";
import { createRoot } from "react-dom/client";

const root = createRoot(document.getElementById('root') as HTMLElement);
root.render(
  <React.StrictMode>
    <div className="flex h-screen">        
        <div className="w-64 bg-gray-900 text-white p-0 border-r border-white/10">
            <h1 className='font-bold text-2xl text-blue-400 p-4 pb-2'>Hello react!</h1>
            <p className='text-grey-800 px-4 pb-4'>This is a beautiful app written with react.</p> 
            <aside className="w-64 bg-gray-900 text-white border-r border-white/10 ">
                <nav className="flex flex-col">
                <a href="#" className="block w-full px-4 py-3 text-gray-400  hover:bg-gray-700 transition">
                    Dashboard
                    </a>
                    <a href="#" className="block w-full px-4 py-3 text-gray-400  hover:bg-gray-700 transition">
                    Project
                    </a>
                    <a href="#" className="block w-full px-4 py-3 text-gray-400  hover:bg-gray-700 transition">
                    Team
                    </a>
                    <a href="#" className="block w-full px-4 py-3 text-gray-400  hover:bg-gray-700 transition">
                    Settings
                    </a>
                </nav>
            </aside>            
        </div>        
        <div className="flex-1 bg-gray-900 text-gray-400 p-4">
            This is your content...
        </div>
    </div>
  </React.StrictMode>
);
Enter fullscreen mode Exit fullscreen mode

By now, the file structure should be:

File structure

When you run your app with the boring npm start command, the resulting app should be something like this:

Screenshot of the Electron app running

Enjoy!
Tante belle cose.

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay