Packaged Electron app and launching a express app as a child process

Viewed 262

Structure

Cloud
  -solution
     -app
     -common
       -index.js

When packaged, the app is in the asar file and i'm not sure how to reference the index.js file in the common folder where the server lives.

In development mode I exec a npm script that goes into the common folder and starts the server.

        exec('cd ../ && cd common && npm run server', (err,stdout,stderr) => {
    .....
if(err){
log.info(error.stack);
        log.info('Error code: ' + error.code);}
    }

I get this error in the exec callback when I try to run the script in production

Error: Command failed: npm run server-production
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path C:\Users\Ibrah\AppData\Local\Programs\Cloud\package.json
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\Ibrah\AppData\Local\Programs\Cloud\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

How can I reference the packaged app server file ?

1 Answers

I'm using both electron-forge and electron-builder to package and build the installer. What I did in my builder config was I included the extraResources key that makes available the specified folder(s) in the dist/{os}-unpacked/resources folder alongside the asar file.

electron-builder

"extraResources": [
  {
    "from": "../@cloud/common",
    "to": "common"
  }
],

Now when I exec the node script that is responsible for starting the server it points to the newly moved folder and which contains the script I'm executing. What's important is that I had to set the current working directory to

const runExpress = exec(' npm run server',{cwd:`../cloud/resources/common/`}, (error: any, stdout: any, stderr: any) => {
      if (error) {
        log.info(error.stack);    
        log.info('Error code: ' + error.code);
        log.info('Signal received: ' + error.signal);
        return;
      }
      else {
        log.info('Child Process STDOUT: ' + stdout);
        log.info('Child Process STDERR: ' + stderr);
      }
    });

My final remarks

Since I'm adding a folder to the accessible application directory User/AppData/Programs/YOUR_APPLICATION_NAME/ the source code becomes available to the user. Is that bad? or could I reference the script from inside the app.asar file (I was searching inside the app.asar for my server folder logic and found the package.json exist but without the scripts strangely enough). Would be great if someone can follow up to this.

Related