我有一个简单的方法,使用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/rimrafNow, 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.
更多推荐
发布评论