frankhale / electron-with-express

A simple app that demonstrates spawning an Express app from Electron
MIT License
662 stars 145 forks source link

Node server not running when built with asar #9

Closed arrie1992 closed 6 years ago

arrie1992 commented 6 years ago

Hi,

I did insert the following lines of code: const app = require('electron').remote.app; const node = spawn(".\\node.exe", ["./express-app/bin/www"], { cwd: app.getAppPath() });

Im running the following command to package: electron-packager . --overwrite --platform=win32 --arch=x64 --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Tutorial App\" --asar

When I run the program, the node server isnt starting..

When I'm running the following command ( no asar flag): electron-packager . --overwrite --platform=win32 --arch=x64 --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Tutorial App\"

It runs fine.. What im doing wrong?

Thank you in advance!

frankhale commented 6 years ago

What im doing wrong?

I have no idea as I have never used ASAR to package my code. I'll take a look but I cannot guarantee when this will be. I'll try to get to this soon.

frankhale commented 6 years ago

Just one more quick point. I am definitely interested in this scenario, I'll get to looking at this.

arrie1992 commented 6 years ago

Thank you!

frankhale commented 6 years ago

I'm looking into this today

frankhale commented 6 years ago

Okay I got this scenario to work. Will post solution soon.

frankhale commented 6 years ago

Okay before I post my final solution to this there is one caveat, instead of using a copy of node like we are doing now to spawn Express in a new process. We will just use the Electron application itself to spawn a new process for the Express server. I've tried to use Node but it doesn't seem to work.

While I'd like to ship my own Node in this scenario I can live with Electron functioning in the same fashion.

frankhale commented 6 years ago

In this scenario you will not need to have Node.exe and Node.lib like the README states. Using child_process.fork instead of child_process.spawn allows our code to work in exactly the same way but Electron will be used to spawn a new process for the Express server instead of our copy of Node.

In index.html around line 64 change the code to:

app = require('electron').remote.app,
node = require("child_process")
  .fork(`${app.getAppPath()}/express-app/bin/www`,
    [], {
      stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
    })

You can then package the code up using the command line (copied from above)

electron-packager . --overwrite --platform=win32 --arch=x64 --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Tutorial App\" --asar
frankhale commented 6 years ago

I'll update the README to document this scenario. Thanks for filing this issue. If you have any questions please let me know.

arrie1992 commented 6 years ago

Great, i'll try it out and let you know! Thank you very much!

arrie1992 commented 6 years ago

Frank,

It's working! Thank you very much

frankhale commented 6 years ago

Your welcome.

orensharon commented 5 years ago

In this scenario you will not need to have Node.exe and Node.lib like the README states. Using child_process.fork instead of child_process.spawn allows our code to work in exactly the same way but Electron will be used to spawn a new process for the Express server instead of our copy of Node.

In index.html around line 64 change the code to:

app = require('electron').remote.app,
node = require("child_process")
  .fork(`${app.getAppPath()}/express-app/bin/www`,
    [], {
      stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
    })

You can then package the code up using the command line (copied from above)

electron-packager . --overwrite --platform=win32 --arch=x64 --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Tutorial App\" --asar

I am using electron-packager to pack my app and I want to use it with asar flag. I followed your instructions but after Electron is up, it seems like Express process is not forked. There aren't error messages but nothing happens (nothing but Electron windows showing up). I also tried to access the express-app from the browser but it doesn't work.

const fork = require("child_process").fork
const electron = require("electron")
const app = electron.app

function startServer() {
    serverProcess = fork(``${app.getAppPath()}/express-app/index.js`, args, {
        stdio: 'pipe',
        env: {
            RESOURCES_PATH: config.resourcesFolder,
            USER_DATA: config.userData,
            SERVER_PORT: expressAppConfig.port
        }
    })
    redirectOutput(serverProcess.stdout);
    redirectOutput(serverProcess.stderr);
}

Any suggestions? Thanks


Update

Found the problem, I had to rebuild one of the deps: > ..\node_modules\.bin\electron-rebuild -f -w better-sqlite3

frankhale commented 5 years ago

thank you for contributing this!