I want to write a file to the current script folder's parent folder (and sometimes subdirectories to that parent folder)?
How should I write the path?
Can this work?
fs.writeFile(__dirname + '../sibling_dir/file.txt', 'test');
The code is as follows: var fs = require('fs'); fs. writeFile("tmp/test. txt", "Hey there!", function(err) { if(err) { console.
Create Files The File System module has methods for creating new files: fs.appendFile() fs.open() fs.writeFile()
The simplest way, and often the most appropriate, is to use the writeFile method in the fs module. This allows you to write to a specified file path, with asynchronous behavior, and creates a new file or replaces one if it exists at the path.
Yes, that should work fine. The main issue I see is that you have no /
between the dirname and the path.
So what you have now is more like:
fs.writeFile('/tmp/module../sibling_dir/file.txt', 'test');
try this:
fs.writeFile(__dirname + '/../sibling_dir/file.txt', 'test');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With