Ibra
Ibra

Reputation: 1072

How to kill node process in a electron application

I'm executing a node.js script that runs an express server when electron application window opens but when I close the app, the server still running which causes an error next time it opens up.

Main Window process

  mainWindow.once('ready-to-show', () => {
const runExpress = exec('npm run server-prodcution', (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);
  }
})

runExpress.on('exit', function (code) {
  runExpress.kill()
  log.info('Child process exited with exit code using on.exit ' + code);
});

.....

Express App:6001

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

process.on('SIGTERM', () => {
  app.close(() => {
    console.log('Process terminated')
  })
})

Error Logs

15:03:32.196 > Child process exited with exit code using on.exit 1
15:03:32.198 > Error: Command failed: npm run server-prodcution
Wed, 09 Mar 2022 20:03:32 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at dist\index.js:68:9
Wed, 09 Mar 2022 20:03:32 GMT body-parser deprecated undefined extended: provide extended option at node_modules\body-parser\index.js:105:29
events.js:377
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::6001
    at Server.setupListenHandle [as _listen2] (net.js:1331:16)
    at listenInCluster (net.js:1379:12)
    at Server.listen (net.js:1465:7)
    at Function.listen (C:\Users\Ibrah\OneDrive\Desktop\CloudReignMVP\solutions\@cloudreign\common\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (C:\Users\Ibrah\OneDrive\Desktop\CloudReignMVP\solutions\@cloudreign\common\dist\index.js:69:5)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1358:8)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  code: 'EADDRINUSE',
  errno: -4091,
  syscall: 'listen',
  address: '::',
  port: 6001
}

Upvotes: 0

Views: 1995

Answers (2)

Ibra
Ibra

Reputation: 1072

I ended up creating a endpoint /abort and whenever the user clicks on the exit button I send a request to close it.

app.post('/abort', (req:any, res:any, next:any) => {
  process.exit()
});

Upvotes: 0

kevin wang
kevin wang

Reputation: 191

you may need to kill Express server when mainWindow closed or electron app quit, like this:

mainWindow.on('closed', () => {
  runExpress.kill();
});

app.on('quit', () => {
  runExpress.kill();
});

also you could detect-port before start Express server, to avoid server port being occupied.

Upvotes: 1

Related Questions