how to write file to parent folder with fs of nodejs?

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');

I tried this;

fs.writeFile('../test.txt', 'test');

that works!

http://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback

fs.write(fd, buffer, offset, length, position, callback)# Write buffer to the file specified by fd.

offset and length determine the part of the buffer to be written.

position refers to the offset from the beginning of the file where this data should be written. If position is null, the data will be written at the current position. See pwrite(2).

The callback will be given three arguments (err, written, buffer) where written specifies how many bytes were written from buffer.

Note that it is unsafe to use fs.write multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream is strongly recommended.

On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.

Tags:

Node.Js