Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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');
like image 635
AGamePlayer Avatar asked Jan 07 '14 05:01

AGamePlayer


People also ask

How do you write a file inside a folder in node JS?

The code is as follows: var fs = require('fs'); fs. writeFile("tmp/test. txt", "Hey there!", function(err) { if(err) { console.

How do I create a file using fs module?

Create Files The File System module has methods for creating new files: fs.appendFile() fs.open() fs.writeFile()

Which method of fs module is used to write a file in node JS?

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.


1 Answers

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');
like image 144
loganfsmyth Avatar answered Oct 21 '22 11:10

loganfsmyth