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
18
votes
6 answers

Node.js: How to check if folder is empty or not with out uploading list of files

I am using Node.js. I want to check if folder is empty or not? One option is to use fs.readdir but it loads whole bunch of files into an array. I have more than 10000 files in the folder. Loading files name is useless just to check if folder is…
jsist
  • 5,223
  • 3
  • 28
  • 43
18
votes
2 answers

Callback function after image has downloaded

I'm trying to save an image download with the request module. With this request('http://google.com/images/logos/ps_logo2.png').pipe(fs.createWriteStream('doodle.png')); It works fine. But I want to be able to do something else after the image…
saeed
  • 3,861
  • 3
  • 25
  • 23
16
votes
5 answers

Module not found: Error: Can't resolve 'fs' in React

I want to implement a SASS compiler for my online editor, for this I want to use the npm package "node-sass". However I get the error Module not found: Error: Can't resolve 'fs' in '\node_modules\fs.realpath'. I already tried to…
DeadApe
  • 416
  • 1
  • 3
  • 10
16
votes
3 answers

How to write a file to specific directory in NodeJS?

I am writing some text to a file us the fs module. fs.writeFile('result.txt', 'This is my text', function (err) { if (err) throw err; console.log('Results Received'); }); Now…
Danyal Ahmad
  • 163
  • 1
  • 1
  • 6
16
votes
1 answer

Difference between readFileSync and using promisify on top of readFile with async/await

Out of curiosity, i want to know if there is any difference between the two. readFileSync: function parseFile(filePath) { let data = fs.readFileSync(filePath); } readFile with promisify: const readFilePromise =…
16
votes
1 answer

Delete a file after download it using nodejs

I'm using Node.js and I need to delete a file after user download it. Is there any way to call a callback method after pipe process ends, or any event for that purpose? exports.downloadZipFile = function(req, res){ var fileName =…
Ragnar
  • 4,393
  • 1
  • 27
  • 40
16
votes
1 answer

How efficient is Chokidar (Node.js)?

I have a caching engine on the server which caches all files accessed under a root directory. I'm thinking of using Chokidar to watch the entire directory tree (recursively) for file changes and update the cache accordingly. But I'm concerned about…
Jon
  • 1,224
  • 2
  • 14
  • 23
15
votes
2 answers

Why doesn't 'fs' work when imported as an ES6 module?

Why do I get errors like these when I try to use the new Node.js support for ES6 modules (e.g. with node --experimental-modules script.mjs)? // script.mjs import * as fs from 'fs'; // TypeError: fs.readFile is not a function fs.readFile('data.csv',…
Qwertie
  • 16,354
  • 20
  • 105
  • 148
15
votes
2 answers

How to return a promise when writestream finishes?

I have such a function, which create a write stream and then write the string array into the file. I want to make it return a Promise once the writing is finished. But I don't know how I can make this work. function writeToFile(filePath: string,…
Lubor
  • 989
  • 3
  • 10
  • 33
15
votes
1 answer

NodeJS File Statistics

I don't know if this is a valid question but is there a documentation out there describing each property from the result of fs.stat() in nodejs. Because I am trying to find the meaning of each of those properties but no luck. Thanks!
Richeve Bebedor
  • 2,138
  • 4
  • 26
  • 40
14
votes
4 answers

NodeJS fs.open failing on existing file (not a path issue)

I've been dealing with this for a long time, so any help is much appreciated. So, I'm downloading a file and saving it using PhantomJS and CasperJS. Let me point out that they aren't the issue. The file is downloaded without a problem. The problem…
Brandon
  • 1,399
  • 4
  • 20
  • 37
14
votes
1 answer

How to close fs.watch listener for a folder

I have an application that allows a user to continuously upload the contents of a local folder to a folder in the cloud. The application is built with electron and node.js So the code that I need help with is this: fs.watch(getHomeDirectory(),…
Grant Herman
  • 923
  • 2
  • 13
  • 29
14
votes
1 answer

Node - Can't seek audio stream

I have created a simple server that uses the fs module to stream an mp3 file to a browser which plays it in an html5 audio element. As it is, the audio streams perfectly fine, however, I can't seek through the stream, even if the part I seek to has…
Alex Wohlbruck
  • 1,340
  • 2
  • 25
  • 41
13
votes
2 answers

fs.writeFile() writes [object, object] instead of actual objects when closing script

My script needs to read and write from a JSON file. This works without problems. I copy the file locally, edit the object, and write them back out to the file. However, when I close the script with Ctrl+C and check my file it has [object, object]…
brabant
  • 135
  • 1
  • 1
  • 7
13
votes
3 answers

Node: fs write() doesn't write inside loop. Why not?

I want to create a write stream and write to it as my data comes in. However, I am able to create the file but nothing is written to it. Eventually, the process runs out of memory. The problem, I've discovered is that I'm calling write() whilst…
user3574603
  • 3,364
  • 3
  • 24
  • 59