Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js fs.existsSync always false

I have a file on this path: ~/Downloads/flightlog_2017-71-19_19747.txt

But when i try to write or check if it exist:

fs.existsSync('~/Downloads/flightlog_2017-71-19_19747.txt')

It always return false

If I do in terminal:

$ nano ~/Downloads/flightlog_2017-71-19_19747.txt

This works fine

like image 817
Arti Avatar asked Aug 19 '17 16:08

Arti


Video Answer


2 Answers

I don't think using ~ to reference a users' Home directory is supported in Node. As there is no equivalent command in Windows.

You can follow this thread for the complete discussion. https://github.com/nodejs/node/issues/684

For the Time being you can use untildify npm package for your needs.

like image 113
rootkill Avatar answered Oct 01 '22 18:10

rootkill


~ is special to the shell (terminal process), expanding to the current home directory, but it is not special otherwise. You'll need to use an absolute path, or a relative path (relative to the process).

For instance, assuming ~ maps to /home/arti:

fs.existsSync('/home/arti/Downloads/flightlog_2017-71-19_19747.txt')

Or if you're running the process in ~/example, then this relative path would work:

fs.existsSync('../Downloads/flightlog_2017-71-19_19747.txt')
like image 25
T.J. Crowder Avatar answered Oct 01 '22 18:10

T.J. Crowder