Launch VLC from an Electron app

Let's assume that you try to execute VLC from the Electron main process. You can use child_process module:

javascript
const { app, BrowserWindow, dialog } = require('electron');
const { spawn }                      = require('child_process');

function createWindow () {
    
    const win = new BrowserWindow({
        
        width: 800,
        height: 600,
        webPreferences: {
            
          nodeIntegration: true
            
        }
        
    });
    
    win.loadFile('index.html');
    
}

function launchVLC () {
    
    // Specifying path in Windows
    const path = 'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe';
    
    // Launching VLC
    const proc = spawn(`"${path}"`, [`http://46.16.226.181:88/?action=stream`], { shell: true });
    
    
}

app.whenReady().then(createWindow).then(launchVLC);

In Windows, you need to specify the full path to launch the application, while in Linux or macOS, you could do the following:

javascript
function launchVLC () {
    
    const proc = spawn(`vlc`, [`http://46.16.226.181:88/?action=stream`], { shell: true });
    
}

I'm not quite sure if there is a better solution to launch VLC in one line of code that works in Windows, Linux, and macOS. Please, tell me if you find it!

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.