node-webcam icon indicating copy to clipboard operation
node-webcam copied to clipboard

[Windows] Electron-Builder - Cannot find CommandCam.exe

Open pilotkid opened this issue 2 years ago • 1 comments

On windows, after packaged with electron builder I get the error

{
  cmd: "\"C:\\Users\\kp\\AppData\\Local\\Programs\\kiosk\\resources\\app.asar\\node_modules\\node- 
  webcam\\src\\bindings\\CommandCam\\CommandCam.exe\"   /filename test_picture.jpg"
  code: 1
  killed: false
  signal: null
}

pilotkid avatar Oct 23 '21 06:10 pilotkid

Inside your builder configuration add nodeIntegration, externals, and your nodeModulesPath.

nodeIntegration: true,
externals: ['node-webcam'],
nodeModulesPath: ['node_modules'],

Then you can add a hook to onNodeModulesFile to rewrite app.asar to app.asar.unpacked

//Apply patch to fix electron-builder issue
onNodeModuleFile(file) {
    if (!file.includes('node-webcam')) return;
    if (file.includes('\\node-webcam\\src\\webcams\\WindowsWebcam.js')) {
        let str = fs.readFileSync(file, 'utf-8');
        str = str.replace(
            'WindowsWebcam.prototype.generateSh',
            `WindowsWebcam.prototype.bin = WindowsWebcam.prototype.bin.includes('.asar.unpacked') ? WindowsWebcam.prototype.bin : WindowsWebcam.prototype.bin.replace('app.asar', 'app.asar.unpacked').replace(/.unpacked.unpacked/g, '.unpacked');\r\nWindowsWebcam.prototype.generateSh`
        );
        fs.writeFileSync(file, str, 'utf-8');
        console.log(
            '✔️Applied patch to node-webcam file ""./node_modules/src/webcams/WindowsWebcam.js'
        );
    }
}

All put together (in my case in vue.config.js):

pluginOptions: {
    electronBuilder: {
        nodeIntegration: true,
        externals: ['pdf-to-printer', 'node-webcam'],
        nodeModulesPath: ['node_modules'],
        builderOptions: {
            //Apply patch to fix electron-builder issue
            onNodeModuleFile(file) {
                if (!file.includes('node-webcam')) return;
                if (file.includes('\\node-webcam\\src\\webcams\\WindowsWebcam.js')) {
                    let str = fs.readFileSync(file, 'utf-8');
                    str = str.replace(
                        'WindowsWebcam.prototype.generateSh',
                        `WindowsWebcam.prototype.bin = WindowsWebcam.prototype.bin.includes('.asar.unpacked') ? WindowsWebcam.prototype.bin : WindowsWebcam.prototype.bin.replace('app.asar', 'app.asar.unpacked').replace(/.unpacked.unpacked/g, '.unpacked');\r\nWindowsWebcam.prototype.generateSh`
                    );
                    fs.writeFileSync(file, str, 'utf-8');
                    console.log(
                        '✔️Applied patch to node-webcam file "./node_modules/src/webcams/WindowsWebcam.js"'
                    );
                }
            }
        }
    }
}
Created in Q/A style incase anyone else runs across this issue

pilotkid avatar Oct 23 '21 06:10 pilotkid