'How to execute a batch script in resources folder of Electron app packaged with electron-builder

I have successfully built an electron app built with react. I packaged the app with a batch script in the resources folder under a server subdirectory. Wrote a script.js file and included that in the startup of the electron app. In turn, the script.js file calls a batch script that's in the same directory as the batch file.

Below is the script.js file

const { Notification } = require('electron');
const { spawn } = require('child_process');

const setup = async () => {
    const bat = spawn('cmd.exe', ['/c', 'setup.bat']);
    bat.stdout.on('data', (data) => {
        console.log(data.toString());
    });
    
    bat.stderr.on('data', (data) => {
        notify({title: 'Build Server Error', body: data.toString()})
        console.error(data.toString());
    });
    
    bat.on('exit', (code) => {
        console.log(`Child exited with code ${code}`);
    });
}

const notify = ({title, body})=> {
  new Notification({ title, body}).show()
}

module.exports = { setup };

This is my project structure

project structure

This is the file path after installation

File path after intallation

This is how I am importing the setup.js in the windowActions.js

const {setup} = require(path.join(app.getAppPath(), "../server/setup.js"));

// This is how i invoke the setup function
const buildServer = async () => {
    try {
        await setup();
    } catch (err) {
        notify({title: 'Build Server Catch Error', body: err.message})
    }
}

The Notifier gets called with the error setup.bat is not recognized as an internal or external command, operable program or batch file

Executing the script.js outside of electron works perfectly.

I have tried using the path helper function in node but it threw file:// is not recognized as an internal or external command, operable program or batch file`

I appreciate every contribution.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source