Javascript 调用节点fs额外函数copySync()时出现无效回调错误
尝试使用Node.js模块的copySync()函数时出错。我不确定我做错了什么。以下是我正在使用的代码:Javascript 调用节点fs额外函数copySync()时出现无效回调错误,javascript,node.js,Javascript,Node.js,尝试使用Node.js模块的copySync()函数时出错。我不确定我做错了什么。以下是我正在使用的代码: fse = require('node-fs-extra'); function deploy(done) { try { fse.copySync('dir1', 'dir2'); console.log('fse.copySync worked!') } catch (err) { console.log('fse.co
fse = require('node-fs-extra');
function deploy(done) {
try {
fse.copySync('dir1', 'dir2');
console.log('fse.copySync worked!')
} catch (err) {
console.log('fse.copySync threw an error');
console.log(err);
}
done();
}
运行此函数时,我得到以下输出:
fse.copySync threw an error
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
at maybeCallback (fs.js:145:9)
at Object.exists (fs.js:212:3)
at Object.copySync (/var/www/html/dev/plugin-biblica-online-bible/node_modules/node-fs-extra/lib/copy.js:62:23)
at deployPlugin (/var/www/html/dev/plugin-biblica-online-bible/gulpfile.js:134:13)
at deploy-plugin (/var/www/html/dev/plugin-biblica-online-bible/node_modules/undertaker/lib/set-task.js:13:15)
at bound (domain.js:427:14)
at runBound (domain.js:440:12)
at asyncRunner (/var/www/html/dev/plugin-biblica-online-bible/node_modules/async-done/index.js:55:18)
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
code: 'ERR_INVALID_CALLBACK'
}
它似乎在抱怨缺少回调函数。但是根据copySync()函数不接受回调函数
使用其他同步函数(如renameSync()时,我没有看到错误,我可以使用异步函数很好地复制目录:
fse = require('node-fs-extra');
function deploy(done) {
fse.copy('dir1', 'dir2', {}, function (err) {
if (err) {
console.log('fse.copy threw an error');
console.log(err);
return;
}
console.log('fse.copy worked!')
});
done();
}
结果:
fse.copy worked!
如果有必要,deploy()函数将作为一个Gulp任务运行。您的代码正在使用,这是不推荐使用的。您正在阅读名为的包的文档
安装fs extra,然后使用:
const fse = require('fs-extra'); // do this instead of require('node-fs-extra');
// ...
然后,你的代码就可以工作了