0

I'm using the fs filesystem JS module to write to a file. I want this file to be in the ~/Desktop folder, so I do this:

var fs = require('fs');
let a = "text"
fs.writeFile('~/Desktop/output.txt', a, () => {})

This runs with no errors, but I don't see an output.txt file on my Desktop, or anywhere in my system. It works fine when I just do output.txt instead of ~/Desktop/output.txt; it saves the output in a file output.txt in my current directory.

Does anyone know what's happening here?

gkeenley
  • 6,088
  • 8
  • 54
  • 129
  • 1
    I don't think nodejs understands the `~` (correct me if I'm wrong) but as you have tagged, you need to use an absolute path. https://stackoverflow.com/questions/9080085/node-js-find-home-directory-in-platform-agnostic-way you can use this to get the home directory – Sterling Archer Feb 18 '20 at 19:28
  • Thanks to both ^^ that does work. So does the answer below with expand-tilde. – gkeenley Feb 18 '20 at 19:35

1 Answers1

2

Node doesn't expand the bash specific shortcut tilde ("~") to your home directory.

Specify the full path or use a package like expand-tilde.

Andy Ray
  • 30,372
  • 14
  • 101
  • 138