Questions & Answers

Spawning a .lnk windows shortcut with node.js gives "UNKNOWN" error

I'm having a game spawned as a child process. It all works nice and well with the game's original .exe file. However, I'd like it to execute minimized, and the only way to do it as far as I can tell is to create a windows shortcut (.lnk file, in the same directory) and set the launch options to minimized. Node.js doesn't like that:

    throw errnoException(err, 'spawn');

Error: spawn UNKNOWN
    at exports._errnoException (util.js:1026:11)
    at ChildProcess.spawn (internal/child_process.js:313:11)
    at exports.spawn (child_process.js:380:9)
    at ExecDom4Server (D:\Google Drive\Clockwork Hounds stuff\MrClockworkBot\v10
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5)

The game itself has a few specific arguments on graphic settings but none to launch it minimized either. Here's the particular code:

function execDom4Server(name, args)
    var spawn = require('child_process').spawn;
    childrenProcesses[name.toLowerCase()] = spawn(dom4root + "dom4.exe.lnk", args);

I've looked everywhere for this, but I suppose it's fairly specific. I don't know if it's just node.js being unable to handle .lnk files. Any pointers that you can give me would be greatly appreciated!

Answers(2) :

This should work if you use the shell option of spawn()

spawn('application.lnk', args, { shell: true })
2023-01-18 23:10:17
Thanks, I had actually tried that and what it did was instantly close down the instance. It's because when setting shell as true the path was executing within the shell and thus needed extra quotes in the string because of spaces. The other problem now is, despite it now executing the .lnk correctly, it doesn't start minimized, even though the launch option is set in its properties. Any ideas there?
2023-01-18 23:10:17
I can't reproduce that behaviour in Windows 10 Build 14986

Another possible issue: I haven't tested whether it's unique to .lnk and .bat files, but even with {shell:true}, child process spawning seems to immediately exit with code 1 if you try to launch the full path to the file e.g.

const child_process = require('child_process');
let child = child_process.spawn('path/to/shortcut.bat');

Instead, looks like you need to run only the final filename of the shortcut, but set the cwd option to the path at which it resides:

const child_process = require('child_process');
let child = child_process.spawn('shortcut.bat', [], {cwd:'path/to'});

If you're using an actual .lnk (tl;dr: my experiments initially led me to fs write the .lnk file into a corresponding .bat file, which is an entirely viable option for further customizing process launching), then you still need {shell:true} to avoid, at least in my case at present, a errno:-4094 code:'UNKNOWN' syscall: 'spawn' error.

const child_process = require('child_process');
let child = child_process.spawn('shortcut.lnk', [], {cwd:'path/to', shell:true});