John Bent
John Bent

Reputation: 1

Electron app with bundled Node.js server fails to load Vite app on systems without Node.js

I'm developing an Electron application that bundles a Node.js server to serve a Vite-built React application. The goal is for the application to be self-contained and work correctly on systems that may not have Node.js installed. While the app works perfectly on my development system (which has Node.js), when I build and run the application on a clean Windows system (without Node.js) the electron window appears blank; it appears that the local server is not starting.

Details

Project Structure:

I have a main Electron application entry point at index.js.

A separate Node.js server file at server.js using node-static to serve static content in the public folder.

A Vite-built React application whose built assets are located in the public folder.

Bundling Node.js:

I am using electron-builder to package the application and include Node.js. My package.json's "build" section is configured to achieve this, and includes the option asar: false.

Server Launch in index.js:

In index.js I attempt to launch the bundled server using a child process using spawn along with the bundled node executable path using process.execPath, and then wait for a console log message that the server has been launched before launching the electron app.

Problem:

On my development machine, npm start launches both the server and Electron app correctly, and I can access the application via http://localhost:1000 in a browser. The packaged application works correctly in my computer, and loads both the main and also /timer page from a second window, if available.

However, when running the installed application on a clean system (without Node.js), the Electron window appears blank. Additionally, I have confirmed that I cannot access http://localhost:1000 in a browser from this system, which indicates that the server is not launching.

HERE IS THE PROJECT: https://github.com/johnbenet009/flexible-stage-timer

Question

Why does the bundled Node.js server fail to start reliably on systems without Node.js installed? What could be the reasons why the server is not launching, and how can I ensure the Electron window only loads after the server is fully running?

I have tried to bundle Node.js and make a self contained application, but despite doing this it fails to work on a system without nodejs installed. I have also tried to use electronify-server and concurrently but these didn't resolve the issues.

I've attempted several approaches to bundle my Node.js server with my Electron application, aiming for a self-contained, cross-platform app. Specifically:

Bundling with electron-builder: I configured electron-builder in my package.json to bundle Node.js with my Electron application using "asar": false and other appropriate settings. I expected this to make the built application fully independent, including its own Node.js runtime.

Expected: A fully functional application in the dist directory that would run correctly on any Windows system, regardless of whether Node.js was installed.

Result: The application was created successfully, but when running on a computer without Node.js, the electron app showed a blank page, and the local server did not start correctly, preventing access to http://localhost:1000 in a browser.

Direct Server Launch in index.js: I modified the index.js file to use child_process.spawn with process.execPath to start the bundled server directly. I added logic to wait for a "Server running" message in the server's output before loading the Electron window to ensure the server was up before loading the UI.

Expected: The bundled Node.js server should be started and fully running before the Electron application loads any content.

Result: The application was created successfully, but when running on a computer without Node.js, the electron app showed a blank page, and the local server did not start correctly, preventing access to http://localhost:1000 in a browser.

Using electronify-server and concurrently: Initially, I tried using electronify-server and concurrently to launch the electron and server application, but these didn't solve the problem since they also relied on the system's node to launch the server and electron applications, and didn't handle the order of events correctly.

Expected: A system to reliably launch the server and electron app sequentially.

Result: This system worked correctly in my system, but didn't solve the core problem of loading on systems without node.

My ultimate expectation was to have a standalone Electron application that:

Despite these attempts, the bundled Node.js server fails to start consistently, and the Electron window appears blank when ran on systems without node.

Upvotes: 0

Views: 90

Answers (0)

Related Questions