javascript 检查文件是否存在于 Gulp 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30348833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:04:49  来源:igfitidea点击:

Check if file exist in Gulp

javascriptnode.jsgulp

提问by Caio Kawasaki

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 任务中,我知道我可以使用 node 的一些 node 函数,有两个:

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

问题是在节点文档中,说这些功能将被弃用

回答by Alexander

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 calling fs.open(), fs.readFile()or fs.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.access()之前调用来检查文件的可访问性fs.open(), fs.readFile()fs.writeFile()不推荐。这样做会引入竞争条件,因为其他进程可能会在两次调用之间更改文件的状态。相反,用户代码应该直接打开/读取/写入文件,并在文件不可访问时处理引发的错误。

回答by Raulucco

You could add

你可以添加

var f;

try {
  var f = require('your-file');
} catch (error) {

  // ....
}

if (f) {
  console.log(f);
}

回答by Hyman Steam

As of 2018, you can use fs.existsSync():

截至 2018 年,您可以使用fs.existsSync()

fs.exists() is deprecated, but fs.existsSync() is not. The callback parameter to fs.exists() accepts parameters that are inconsistent with other Node.js callbacks. fs.existsSync() does not use a callback.

fs.exists() 已被弃用,但 fs.existsSync() 不是。fs.exists() 的回调参数接受与其他 Node.js 回调不一致的参数。fs.existsSync() 不使用回调。

See this answer for more details.

有关更多详细信息,请参阅此答案。

回答by Ithar

I believe fs-accesspackage has been depreciated alternatively you may want to use:

我相信fs-access包已经折旧了,或者你可能想使用:

path-exists.

path-exists.

file-exists.

file-exists.

Intracutions (path-exists):

Intracutions(路径存在):

npm install path-exists --save

const myFile = '/my_file_to_ceck.html';
const exists = pathExists.sync(myFile);
console.log(exists);

Intracutions (file-exists):

Intracutions(文件存在):

npm install file-exists --save


const fileExists = require('file-exists');
const myFile = '/my_file_to_ceck.html';
fileExists(myFile, (err, exists) => console.log(exists))

NPM Link: path exists

NPM 链接:路径存在

NPM Link: file exists

NPM 链接:文件存在

回答by David Lemon

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 raisedif the file is not available.

To check if a file exists without manipulating it afterwards, fs.access() is recommended.

不推荐在调用 fs.open()、fs.readFile() 或 fs.writeFile() 之前使用 fs.stat() 检查文件是否存在。相反,用户代码应该直接打开/读取/写入文件,并在文件不可用时处理引发的错误

要检查文件是否存在而不对其进行操作,建议使用 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
}