Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to run Express inside an Electron app

How to run Express inside an Electron app

You can run your Express app very easily inside your Electron app.

All you need to do is to:

  • place all the files of your Express app inside a new app folder in your_electron_app\resources\app
  • reconfigure the app.js file
  • refactor some relative pathes in your Express app

Configure app.js

You should start your Express app before opening a new BrowserWindow and the load a new mainWindow like this:

const express = require('./express'); //your express app

app.on('ready', function() {
  express();
  mainWindow = new BrowserWindow({
    width: 1280,
    height: 720,
    autoHideMenuBar: true,
    useContentSize: true,
    resizable: false,
  });
  mainWindow.loadURL('http://localhost:5000/');
  mainWindow.focus();

});

Refactor pathes

Pathes in Electron don't work the same way they do in your Express app.

You have to make them all from relative to absolute pathes first.

So instead of doing this:

app.set('views', '/client/views');
app.use(express.static(/client/dist/static));

you have to do this:

app.set('views', __dirname + '/client/views');
app.use(express.static(__dirname + '/client/dist/static'));
@ashish-1689
Copy link

ashish-1689 commented Sep 24, 2020

For me, the build was failing because one of the devDependency was not getting bundled, so I moved it to the dependencies and it started working.
Just build the app and run locally to see the error.

@Brijeshlakkad
Copy link

Brijeshlakkad commented Dec 12, 2020

@dcooney
You can get a port that is available at production time by using the get-port-electron

@skandonkumar
Copy link

skandonkumar commented Mar 17, 2021

How do I send the data from express app to main.js? For example, I use express to receive data to PORT 3000 in app.js. Now I send data from external device to PORT 3000 and I want to send that data to main.js. Is there a way to do this?

@Brijeshlakkad
Copy link

Brijeshlakkad commented Mar 17, 2021

Yes, you can have a class that extends EventEmitter and just create a single instance of that class. Use this instance everywhere and listen to the event.

The second way: You can import a function into the file which has express Js from main.js which can be called to manipulates the data of main.js.

@skandonkumar
Copy link

skandonkumar commented Mar 17, 2021

Yes, you can have a class that extends EventEmitter and just create a single instance of that class. Use this instance everywhere and listen to the event.

The second way: You can import a function into the file which has express Js from main.js which can be called to manipulates the data of main.js.

I will try the EventEmitter. Thank You

@chriszrc
Copy link

chriszrc commented Sep 14, 2022

Starting express in a forked child process works great when you want to keep the main thread from freezing:

const { fork } = require("child_process");

  let ps;
  //...
  ps = fork(`${__dirname}/../out-tsc/server-app/main.js`, [], {
    cwd: `${__dirname}/../`,
  });

@Nestoter
Copy link

Nestoter commented Sep 20, 2022

Starting express in a forked child process works great when you want to keep the main thread from freezing:

const { fork } = require("child_process");

  let ps;
  //...
  ps = fork(`${__dirname}/../out-tsc/server-app/main.js`, [], {
    cwd: `${__dirname}/../`,
  });

You are the man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment