Questions tagged [fs]

File I/O in the Node.js Javascript platform

File I/O in the platform.

The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.

To use this module:

const fs = require('fs');

All file system operations have synchronous and asynchronous forms.

The asynchronous form always takes a completion callback as its last argument. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation was completed successfully, then the first argument will be null or undefined.

Documentation.

2942 questions
2069
votes
18 answers

Writing to files in Node.js

I've been trying to find a way to write to a file when using Node.js, but with no success. How can I do that?
Gjorgji
  • 22,458
  • 10
  • 31
  • 39
1486
votes
18 answers

Check synchronously if file/directory exists in Node.js

How can I synchronously check, using node.js, if a file or directory exists?
user196106
754
votes
18 answers

How to append to a file in Node?

I am trying to append a string to a log file. However writeFile will erase the content each time before writing the string. fs.writeFile('log.txt', 'Hello Node', function (err) { if (err) throw err; console.log('It\'s saved!'); }); // =>…
supercobra
  • 15,810
  • 9
  • 45
  • 51
650
votes
31 answers

How to download a file with Node.js (without using third-party libraries)?

How do I download a file with Node.js without using third-party libraries? I don't need anything special. I only want to download a file from a given URL, and then save it to a given directory.
greepow
  • 7,003
  • 4
  • 17
  • 8
568
votes
9 answers

Node.js check if path is file or directory

I can't seem to get any search results that explain how to do this. All I want to do is be able to know if a given path is a file or a directory (folder).
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
452
votes
9 answers

Get file name from absolute path in Nodejs?

How can I get the file name from an absolute path in Nodejs? e.g. "foo.txt" from "/var/www/foo.txt" I know it works with a string operation, like fullpath.replace(/.+\//, ''), but I want to know is there an explicit way, like file.getName() in Java?
fxp
  • 6,792
  • 5
  • 34
  • 45
335
votes
22 answers

Node.js check if file exists

How do I check for the existence of a file?
RomanGor
  • 3,835
  • 2
  • 19
  • 26
311
votes
9 answers

Write / add data in JSON file using Node.js

I am trying to write JSON file using node from loop data, e.g.: let jsonFile = require('jsonfile'); for (i = 0; i < 11; i++) { jsonFile.writeFile('loop.json', "id :" + i + " square :" + i * i); } outPut in loop.json is: id :1 square : 1 but I…
Isoftmaster
  • 3,175
  • 2
  • 11
  • 15
279
votes
30 answers

Copy folder recursively in Node.js

Is there an easier way to copy a folder and all its content without manually doing a sequence of fs.readir, fs.readfile, fs.writefile recursively? I am just wondering if I'm missing a function which would ideally work like…
lostsource
  • 21,070
  • 8
  • 66
  • 88
273
votes
23 answers

How to create full path with node's fs.mkdirSync?

I'm trying to create a full path if it doesn't exist. The code looks like this: var fs = require('fs'); if (!fs.existsSync(newDest)) fs.mkdirSync(newDest); This code works great as long as there is only one subdirectory (a newDest like 'dir1')…
David Silva Smith
  • 11,498
  • 11
  • 67
  • 91
224
votes
12 answers

Using filesystem in node.js with async / await

I would like to use async/await with some filesystem operations. Normally async/await works fine because I use babel-plugin-syntax-async-functions. But with this code I run into the if case where names is undefined: import fs from 'fs'; async…
Quellenangeber
  • 2,241
  • 2
  • 9
  • 3
146
votes
5 answers

Writing JSON object to a JSON file with fs.writeFileSync

I am trying to write a JSON object to a JSON file. The code executes without errors, but instead of the content of the object been written, all that gets written into the JSON file is: [object Object] This is the code that actually does the…
Romulus3799
  • 1,827
  • 3
  • 15
  • 20
131
votes
10 answers

How to close a readable stream (before end)?

How to close a readable stream in Node.js? var input = fs.createReadStream('lines.txt'); input.on('data', function(data) { // after closing the stream, this will not // be called again if (gotFirstLine) { // close this stream and…
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
124
votes
4 answers

Looping through files in a folder Node.JS

I am trying to loop through and pick up files in a directory, but I have some trouble implementing it. How to pull in multiple files and then move them to another folder? var dirname = 'C:/FolderwithFiles'; console.log("Going to get file…
Prolasis
  • 1,323
  • 2
  • 11
  • 8
119
votes
2 answers

NodeJS accessing file with relative path

It seemed like a straight forward problem. But I amn't able to crack this. Within helper1.js I would like to access foobar.json (from config/dev/) root -config --dev ---foobar.json -helpers --helper1.js I couldn't get this to work fs:…
lonelymo
  • 3,972
  • 6
  • 28
  • 36
1
2 3
99 100