javascriptfsabsolute-pathpathname

Javscript fs module: where is output being written to?


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?


Solution

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

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