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
22
votes
2 answers

Node.js fs module and windows paths

Does the Node.js fs module implicitly convert Windows folder path separators from '\\' to '/'? For example, if I use this call on Windows: fs.readdirSync(dir).forEach(function(file) { }); file argument has '/' path separators, not '\\', why is…
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
21
votes
2 answers

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory when processing large files with fs

I have a nodeJs script that process a bunch of large .csv files (1.3GB for all). It run for a moment and throw this error: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory I have tried to put…
TOPKAT
  • 6,667
  • 2
  • 44
  • 72
21
votes
2 answers

Can't get array of fs.Dirent from fs.readdir

I am using node 8.10.0. fs.readdir() returns an array of filenames and child directory names, or fs.Dirents[]. I can't get that to work. Here's a simple example: console.log(require("fs").readdirSync("/", {withFileTypes:true})); This gives me an…
lonix
  • 14,255
  • 23
  • 85
  • 176
21
votes
2 answers

ENOENT error when using fs.writeFile

Trying to write to a file using fs.writeFile into a sibling directory. This works fine when using Sitemap.xml into the same directory, but not with the relative path. The public directory exists and it gives the same error whether or not Sitemap.xml…
Tom
  • 1,546
  • 2
  • 20
  • 42
20
votes
3 answers

Angular 7 how to use fs module?

I'm using Angular 7. I tried to use fs module on Typescript for open a directory. I always have this error: "Module not found: Error: Can't resolve fs" types@node and file-system are installed. My code: import {Component, OnInit} from…
Eng
  • 1,617
  • 2
  • 12
  • 25
20
votes
5 answers

How to get directory size in node.js without recursively going through directory?

How do I get the size of a directory in node.js without recursively going through all the children in a directory? E.g. var fs = require('fs'); fs.statSync('path/to/dir'); Will return me an object like this, { dev: 16777220, mode: 16877, nlink:…
user772401
  • 2,754
  • 3
  • 31
  • 50
19
votes
5 answers

Error [ERR_STREAM_WRITE_AFTER_END]: write after end

Code explanation: I am returning specific HTML file when user visit specific url: const http = require('http'); const fs = require('fs'); fs.readFile('./funkcionalnosti-streznika.html', function(err1, html1) { …
Tadej
  • 301
  • 1
  • 2
  • 5
19
votes
1 answer

writeFile does not create file

Error code looks like: { Error: ENOENT: no such file or directory, open 'sad' errno: -2, code: 'ENOENT', syscall: 'open', path: 'sad' } where 'sad' is the name of file I would like to write to and it doesn't exist. Code looks like…
SubjectX
  • 836
  • 2
  • 9
  • 33
19
votes
1 answer

Promises with fs and bluebird

I'm currently learning how to use promises in nodejs so my first challenge was to list files in a directory and then get the content of each with both steps using asynchronous functions. I came up with the following solution but have a strong…
Henrik
  • 203
  • 1
  • 2
  • 9
19
votes
1 answer

Relative file system write path within module

I have a executable node / javascript script that has a debug boolean, if set to true a couple of files will be written. This executable is also node module. Depending on the working directory of the user running the script it seems that the…
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
18
votes
5 answers

Send MediaRecorder blobs to server and build file on backend

I'm working on a website using nodejs and SailsJs. My objective is send the blobs generated by MediaRecorder.ondataavailable event (which returns small blobs) to the server and after finishing recording build the complete file on the server to store…
jonystorm
  • 548
  • 4
  • 17
18
votes
8 answers

chaining `fs.readdir` with a `.then` to return an array

I am trying to create an array of specific files in a directory; which will go through a few test cases to make sure it fits a given criteria. I'm using the fs.readdir method, but it doesn't return a promise meaning I cannot push to an array. My…
grpcMe
  • 1,367
  • 5
  • 15
  • 28
18
votes
3 answers

How to use Typescript Async/ await with promise in Node JS FS Module

How to use Typescript async / await function and return typescript default promises in node js FS module and call other function upon promise resolved. Following is the code : if (value) { tempValue = value; fs.writeFile(FILE_TOKEN,…
Shan Khan
  • 9,667
  • 17
  • 61
  • 111
18
votes
4 answers

In fs.writeFile([option]), how an "options parameter" generally work?

I was reading this document about Node.js file system, fs.writeFile(filename, data, [options], callback). So I noticed that i have seen the [options] pretty often, but never used it for anything. Can someone give me an example? All the cases i had…
iamwave007
  • 333
  • 2
  • 3
  • 10
18
votes
5 answers

fs.writeFile has no errors, but fails to write file

I am using node.js, trying to save a file, no errors are thrown, yes the image won't save. This is how I am saving the file: var url = 'captures/' + getFileName() + '.png'; fs.writeFile(url, base64, 'base64', function(err) { if(err) { …
JohnRobertPett
  • 1,133
  • 4
  • 21
  • 36