我如何删除一个文件与node.js?
http://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback
我没有看到删除命令?
我如何删除一个文件与node.js?
http://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback
我没有看到删除命令?
当前回答
rm -rf它
require("fs").rmSync(file_or_directory_path_existing_or_not, {recursive: true, force: true});
// Added in Node.js 14.14.0.
要求(fs)。rmSync或require("fs").rm。
其他回答
您可以使用del模块删除当前目录中的一个或多个文件。它的好处是可以保护您不删除当前工作目录和以上目录。
const del = require('del');
del(['<your pathere here>/*']).then( (paths: any) => {
console.log('Deleted files and folders:\n', paths.join('\n'));
});
你可以做下面的事情
const deleteFile = './docs/deleteme.txt'
if (fs.existsSync(deleteFile)) {
fs.unlink(deleteFile, (err) => {
if (err) {
console.log(err);
}
console.log('deleted');
})
}
异步删除文件或符号链接。除了一个可能的异常外,没有其他参数被提供给完成回调。
Fs.unlink()将不能在空目录或其他目录上工作。要删除一个目录,请使用fs.rmdir()。
更多的细节
你可以使用fs。Unlink (path, callback)函数。下面是一个带有"error-back"模式的函数包装器示例:
/ /依赖性。 Const fs = require('fs'); //删除文件 const deleteFile = (filePath, callback) => { //断开文件链接。 fs。unlink(filePath, (error) => { If(!错误){ 回调(假); }其他{ callback('删除文件错误'); } }) };
Fs-extra提供了一个删除方法:
const fs = require('fs-extra')
fs.remove('/tmp/myfile')
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
https://github.com/jprichardson/node-fs-extra/blob/master/docs/remove.md
你可以调用fs。unlink(path, callback)用于异步断开(2)或fs.unlinkSync(path)用于同步断开(2)。 path是你要删除的file-path。
例如,我们想从c:/book目录中删除discovery.docx文件。我的文件路径是c:/book/discovery。docx。删除文件的代码是,
var fs = require('fs');
var filePath = 'c:/book/discovery.docx';
fs.unlinkSync(filePath);