Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs in Node.js Doesn't Understand ~/

I'm trying to check if a directory exists as part of a command-line app in node.js. However, fs doesn't seem to understand ~/. For example, the following returns false...

> fs.existsSync('~/Documents')
false

...but this returns true...

> fs.existsSync('/Users/gtmtg/Documents')
true

...even though they're both the same thing.

Why does this happen, and is there are workaround for this? Thanks in advance!

like image 728
gtmtg Avatar asked Sep 16 '12 22:09

gtmtg


1 Answers

That's because ~/ is supported by the command shell, not the file system APIs.

like image 154
JohnnyHK Avatar answered Sep 18 '22 22:09

JohnnyHK