简体   繁体   English

Node.js exec()不执行回调

[英]Node.js exec() doesn't execute callback

I have a simple method that executes exec method from child_process using rm -rf as command. 我有一个简单的方法,使用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/*'})

Method works and files do get deleted, however callback never gets executed. 方法有效并且文件确实被删除,但是回调从未执行。 What am I missing? 我想念什么?

Node version: v6.10.2 节点版本:v6.10.2

First of all, you may want to use one of those modules to remove files in Node: 首先,您可能需要使用其中一个模块来删除Node中的文件:

Now, it's not possible that the console.log('test'); 现在, console.log('test');不可能的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. 不会执行,因为您甚至都不会测试错误,因此即使您在PATH中没有rm命令或它失败了,它也会被执行。

What is possible is that you may not see the console.log() output if the STDOUT is redirected or even the console.log redefined. 如果重定向了STDOUT甚至重新定义了console.log ,则可能看不到console.log()输出。 To really see if the callback is called you may try to write some message to a file. 要真正查看是否调用了回调,您可以尝试将一些消息写入文件。

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

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