Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  38] [ 5]  / answers: 1 / hits: 6384  / 6 Years ago, tue, december 18, 2018, 12:00:00

I am trying to see if a file exists locally like this:



if (exec(`-f ~/.config/myApp/bookmarks.json`)) {
console.log('exists')
} else {
console.log('does not')
}


However, I get exists in the console whether the file exists or not


More From » bash

 Answers
5

You should import the fs module into your code.
If you're running on the mainprocess, then do a simple const fs = require('fs'); but if you're on the renderer process then run const fs = require('electron').remote.require('fs')



Then with the fs module you can run a simple exists method on the file:



if (fs.existsSync(`~/.config/myApp/bookmarks.json`)) {
console.log('exists')
} else {
console.log('does not')
}


Although you really should check for this asynchronously:



fs.access(`~/.config/myApp/bookmarks.json`, (err) => {
if (err) {
console.log('does not exist')
} else {
console.log('exists')
}
})

[#9798] Monday, December 17, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pierce

Total Points: 315
Total Questions: 103
Total Answers: 89

Location: Svalbard and Jan Mayen
Member since Mon, Jun 7, 2021
3 Years ago
;