Node.js exec()不执行回调(Node.js exec() doesn't execute callback)

编程入门 行业动态 更新时间:2024-10-10 08:18:49
Node.js exec()不执行回调(Node.js exec() doesn't execute callback)

我有一个简单的方法,使用rm -rf作为命令从child_process执行exec方法。

const exec = require('child_process').exec const Foo = { emptyDir: params => { exec(`rm -rf ${params.path}`, (err, stdout, stderr) => { console.log('test'); }) } } Foo.emptyDir({path:'./data/*'})

方法工作和文件被删除,但回调永远不会被执行。 我错过了什么?

节点版本:v6.10.2

I have a simple method that executes exec method from child_process using rm -rf as command.

const exec = require('child_process').exec const Foo = { emptyDir: params => { exec(`rm -rf ${params.path}`, (err, stdout, stderr) => { console.log('test'); }) } } Foo.emptyDir({path:'./data/*'})

Method works and files do get deleted, however callback never gets executed. What am I missing?

Node version: v6.10.2

最满意答案

首先,您可能想要使用其中一个模块来删除节点中的文件:

https://www.npmjs.com/package/remove https://www.npmjs.com/package/rimraf

现在, console.log('test'); 不会执行,因为即使您在PATH中没有rm命令或者它失败,您甚至不会测试错误,因此它将被执行。

如果STDOUT被重定向或者甚至重新定义了console.log ,你可能看不到console.log()输出。 要真正查看回调是否被调用,您可以尝试将一些消息写入文件。

First of all, you may want to use one of those modules to remove files in Node:

https://www.npmjs.com/package/remove https://www.npmjs.com/package/rimraf

Now, it's not possible that the console.log('test'); is not executed, because you don't even test for errors so it will be executed even if you don't have the rm command in PATH or if it fails.

What is possible is that you may not see the console.log() output if the STDOUT is redirected or even the console.log redefined. To really see if the callback is called you may try to write some message to a file.

更多推荐

本文发布于:2023-08-07 06:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   js   Node   exec   callback

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!