I need to check if a file exists in a gulp task, i know i can use some node functions from node, there are two:
我需要检查gulp任务中是否存在文件,我知道我可以使用节点中的某些节点函数,有两个:
fs.exists()
and fs.existsSync()
fs.exists()和fs.existsSync()
The problem is that in the node documentation, is saying that these functions will be deprecated
问题是在节点文档中,是说这些函数将被弃用
3 个解决方案
#1
39
You can use fs.access
你可以使用fs.access
fs.access('/etc/passwd', (err) => {
if (err) {
// file/path is not visible to the calling process
console.log(err.message);
console.log(err.code);
}
});
List of available error codes here
此处列出了可用的错误代码
Using
fs.access()
to check for the accessibility of a file before callingfs.open(), fs.readFile()
orfs.writeFile()
is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.在调用fs.open()之前,使用fs.access()检查文件的可访问性,不建议使用fs.readFile()或fs.writeFile()。这样做会引入竞争条件,因为其他进程可能会在两次调用之间更改文件的状态。相反,用户代码应直接打开/读取/写入文件,并处理在文件无法访问时引发的错误。
#2
1
You could add
你可以添加
var f;
try {
var f = require('your-file');
} catch (error) {
// ....
}
if (f) {
console.log(f);
}
#3
0
The node documentatión does not recommend using stat to check wether a file exists:
节点文档不建议使用stat来检查文件是否存在:
Using fs.stat() to check for the existence of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.
在调用fs.open()之前,使用fs.stat()检查文件是否存在,建议不要使用fs.readFile()或fs.writeFile()。相反,用户代码应直接打开/读取/写入文件,并在文件不可用时处理引发的错误。
To check if a file exists without manipulating it afterwards, fs.access() is recommended.
要检查文件是否存在而不事后对其进行操作,建议使用fs.access()。
If you don't need to read or write the file you should use fs.access
, the simple and asynchronous way is:
如果您不需要读取或写入文件,则应使用fs.access,简单和异步方式是:
try {
fs.accessSync(path)
// the file exists
}catch(e){
// the file doesn't exists
}
#1
39
You can use fs.access
你可以使用fs.access
fs.access('/etc/passwd', (err) => {
if (err) {
// file/path is not visible to the calling process
console.log(err.message);
console.log(err.code);
}
});
List of available error codes here
此处列出了可用的错误代码
Using
fs.access()
to check for the accessibility of a file before callingfs.open(), fs.readFile()
orfs.writeFile()
is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.在调用fs.open()之前,使用fs.access()检查文件的可访问性,不建议使用fs.readFile()或fs.writeFile()。这样做会引入竞争条件,因为其他进程可能会在两次调用之间更改文件的状态。相反,用户代码应直接打开/读取/写入文件,并处理在文件无法访问时引发的错误。
#2
1
You could add
你可以添加
var f;
try {
var f = require('your-file');
} catch (error) {
// ....
}
if (f) {
console.log(f);
}
#3
0
The node documentatión does not recommend using stat to check wether a file exists:
节点文档不建议使用stat来检查文件是否存在:
Using fs.stat() to check for the existence of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.
在调用fs.open()之前,使用fs.stat()检查文件是否存在,建议不要使用fs.readFile()或fs.writeFile()。相反,用户代码应直接打开/读取/写入文件,并在文件不可用时处理引发的错误。
To check if a file exists without manipulating it afterwards, fs.access() is recommended.
要检查文件是否存在而不事后对其进行操作,建议使用fs.access()。
If you don't need to read or write the file you should use fs.access
, the simple and asynchronous way is:
如果您不需要读取或写入文件,则应使用fs.access,简单和异步方式是:
try {
fs.accessSync(path)
// the file exists
}catch(e){
// the file doesn't exists
}