fs.exists, fs.existsSync - why are they deprecated?

前端 未结 7 574
遥遥无期
遥遥无期 2021-01-07 20:06

I noticed that the official node documentation says something startling about fs.exists:

\"fs.exists() is an anachronism and exists only

7条回答
  •  情歌与酒
    2021-01-07 20:30

    I think it because it's redundant. You can check if a file exists by trying to open it. It will give you ENOENT if it doesn't exist:

    > fs.open('foo', 'r', function(err, fd) {
        ... console.log(err, fd);
        ... 
    })
    undefined
    > { [Error: ENOENT, open 'foo'] errno: 34, code: 'ENOENT', path: 'foo' } undefined
    

提交回复
热议问题