Electron Path issues after build

33 Views Asked by At

I have a simple Electron app that has an Express instance running inside of it. Everything works as expected in development, but as soon as I build it on Mac or Windows I start getting path errors:

Main.js

import path from 'node:path'
import express from 'express'
import axios from 'axios'
import asyncHandler from 'express-async-handler'
import dotenv from 'dotenv'
import fs from 'fs'

dotenv.config()
const expressApp = express()
expressApp.set('view engine', 'ejs')
var port = 8080
var token = null
var v = null
var accessKey = null
var __dirname = path.resolve()

function createWindow() {
    const win = new BrowserWindow({
        width: 250,
        height: 300,
        webPreferences: {
            preload: 'preload.js',
        },
    })

    win.loadURL(`http://localhost:${port}`)
    win.webContents.openDevTools({ mode: 'bottom' })
}

app.on('ready', () => {})

app.whenReady().then(() => {
    createWindow()
    Menu.setApplicationMenu(
        Menu.buildFromTemplate([
            {
                role: 'appMenu',
                submenu: [
                    {
                        label: 'relaunch(); exit()',
                        click() {
                            app.relaunch()
                            app.exit()
                        },
                    },
                ],
            },
        ])
    )

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow()
        }
    })
})

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

expressApp.get('/', (req, res) => {
    res.render(`landing`)
})

expressApp.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

Basically my Electron app calls win.loadURL and points back to my Express instance to load the / route. That route returns an EJS file which lives in views/landing.ejs. When I build the file I get errors that it can not find the landing file. I have tried every combination of __dirpath and path.joins inside the render call but can not get this to load. Any ideas would be appreciated!

0

There are 0 best solutions below