javascript - Why does fs.existsSync always return false? - Stack Overflow

I'm trying to check if a file is exist. So, I apply fs.existsSync() function.But, the problem is

I'm trying to check if a file is exist. So, I apply fs.existsSync() function. But, the problem is this function always return false (not exit).

Written below is my code.

Thank you in advance!

[ My Environment ]

  • Ubuntu
  • Node.js version 16.14.0
const fs = require('fs')

const getProvinceByCountry = country => {

    const filePath = `../../config/province/${country}.json`
    const fileExist = fs.existsSync(filePath)

    if (!fileExist) { 
        console.log('File does not exist') 
    } else {
        console.log('File exists') 
    }
}

// Result : File does not exists.
// existsSync() always returns false even if the path and the file name are correct.

I'm trying to check if a file is exist. So, I apply fs.existsSync() function. But, the problem is this function always return false (not exit).

Written below is my code.

Thank you in advance!

[ My Environment ]

  • Ubuntu
  • Node.js version 16.14.0
const fs = require('fs')

const getProvinceByCountry = country => {

    const filePath = `../../config/province/${country}.json`
    const fileExist = fs.existsSync(filePath)

    if (!fileExist) { 
        console.log('File does not exist') 
    } else {
        console.log('File exists') 
    }
}

// Result : File does not exists.
// existsSync() always returns false even if the path and the file name are correct.

Share Improve this question edited Mar 24, 2022 at 15:25 Maker asked Mar 24, 2022 at 14:37 MakerMaker 1711 silver badge12 bronze badges 6
  • 1 Did you try to console.log() the file path to make sure it is the right one, since you are relying on a relative path? – Ben Commented Mar 24, 2022 at 14:41
  • Isn't it working correctly? If the file exists it will always go to else block because your if condition is !fileExist. If file doesn't exist it will go to if block. I don't understand the issue here? Even your console log says - File exists – Shivam Commented Mar 24, 2022 at 15:14
  • @ShivamSood oh... my mistake. The result was 'File does not exist' not 'File exist' – Maker Commented Mar 24, 2022 at 15:27
  • @Ben the file path is correct. – Maker Commented Mar 24, 2022 at 15:28
  • There doesn't seem to be too many variables in this issue, one of it has to be incorrect. Either Path is incorrect. Country parameter doesn't have correct value. Try proving absolute path and console.log the country parameter to check if you are getting correct value. – Shivam Commented Mar 24, 2022 at 15:34
 |  Show 1 more ment

1 Answer 1

Reset to default 4

Finally, I've solve the problem. What I did is use path.join.

const filePath = path.join(__dirname, `../../config/province/${country}.json`)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248933a4618557.html

相关推荐

  • javascript - Why does fs.existsSync always return false? - Stack Overflow

    I'm trying to check if a file is exist. So, I apply fs.existsSync() function.But, the problem is

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信