简体   繁体   English

NodeJS - Spawn功能

[英]NodeJS - Spawn function

I'm working with nodejs and the child process module to execute commands on my platform. 我正在使用nodejs和子进程模块在我的平台上执行命令。 To do that, I use the spawn function . 为此,我使用spawn函数

Here's my code: 这是我的代码:

var spawn_execution = executor.spawn(command, args);

spawn_execution.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

spawn_execution.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});

Nothing fancy. 没有什么花哨。 So I tried a couple of commands that worked like 所以我尝试了几个有效的命令

executor.spawn('C:/path/to/ffmpeg.exe', [...]);

But when I try to use a native windows command, it does not work. 但是当我尝试使用本机Windows命令时,它不起作用。 For instance, I tried: 例如,我试过:

executor.spawn('del', ['C:\\my\\file\\to\\delete']);

When executing this, I've got a ENOENT error which means that the file is not found. 执行此操作时,我遇到ENOENT错误,表示找不到该文件。 So I did another thing: 所以我做了另一件事:

executor.spawn('C:/my/script-delete.exe', ['C:\\my\\file\\to\\delete']);

This script-delete.exe just contains: 这个script-delete.exe只包含:

del %1

So why does the spawn function need to have a script file? 那么为什么spawn函数需要有一个脚本文件呢? Why does it not work with native windows command? 为什么它不适用于本机Windows命令? Do you know a way to make it work with a native command? 你知道一种使用本机命令工作的方法吗?

Thank you! 谢谢!

It doesn't work as the internal commands can't be found from executor.spawn only cmd.exe knows them. 它不起作用,因为只有cmd.exe知道它们才能从executor.spawn找不到内部命令。

So it works from a batch file or when you use something like 所以它适用于批处理文件或使用类似的东西

executor.spawn('cmd.exe', ['/C', 'del', 'C:\\my\\file\\to\\delete']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM