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
13
votes
3 answers

How can I lock a file while writing to it asynchronously

I've got two node threads running, one watching a directory for file consumption and another that is responsible for writing files to given directories. Typically they won't be operating on the same directory, but for an edge case I'm working on…
andrew.carpenter
  • 516
  • 2
  • 6
  • 14
13
votes
2 answers

fs.writeFileSync gives Error:UNKNOWN, correct way to make synchronous file write in nodejs

I have a NodeJS server application. I have this line of code for my logging: fs.writeFileSync(__dirname + "/../../logs/download.html.xml", doc.toString()); Sometimes it works correctly, but under heavy load it gives this exception: Error:…
Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206
13
votes
2 answers

node.js make code wait until the fs.readFile is completed

I got a problem in node.js file system. here is my code. my function always return a empty string. I'm wondering is there anyway to make my function stop execute until readFile method is completed. var fs = require('fs'); function myfun(filePath){ …
Deryckxie
  • 355
  • 1
  • 3
  • 9
13
votes
4 answers

Create a file if it doesn't already exist

I would like to create a file foobar. However, if the user already has a file named foobar then I don't want to overwrite theirs. So I only want to create foobar if it doesn't exist already. At first, I thought that I should do…
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
13
votes
4 answers

Removing the last character from file stream in node.js (fs module)

Using node.js, I am trying to build an array of objects and write them to a file. To do this, I'm using the built in fs library. After calling var file = fs.createWriteStream('arrayOfObjects.json'); and file.write('[') I run several asynchronous…
michaelgmcd
  • 2,369
  • 1
  • 18
  • 24
13
votes
1 answer

How can I change a specific line in a file with node js?

I want to change a specific line in a text file using node js with the fs module. Is there a more elegant way then loading the file into an array? Old File: Line 1 Line 2 Line 3 New File: Line 1 something new Line 3 Thanks for your replies!
user4986573
  • 163
  • 2
  • 5
12
votes
1 answer

Why is fs.readFileSync() faster than await fsPromises.readFile()?

Here is the test code (in an express environment just because that's what I happen to be messing around with): const fs = require('fs-extra'); const fsPromises = fs.promises; const express = require('express'); const app = express(); const…
drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
12
votes
2 answers

Axios: data should be a string, Buffer or Uint8Array

I'm getting this error when trying to do a POST request using axios: TypeError: data should be a string, Buffer or Uint8Array Here is my code snippet: var fs = require('fs'), axios = require('axios'); var FormData = require('form-data'); var form…
Pravin Bhapkar
  • 463
  • 1
  • 5
  • 15
12
votes
2 answers

Vue.js webpack : how to get the list of files in a directory?

I am trying to get a list of all files in the 'assets/illustrations' directory of a SPA static app as a JSON object to be re-used in my Vuex store the assets/illustrations dir structure is : assets illustrations ill-1 prop-1-1.jpg …
user762579
12
votes
3 answers

How to convert the string type from an API response to an image file - ����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001 -

I have used https://graph.microsoft.com/beta/me/photo/$value API to get the profile picture of the outlook user. I get an image on running the above API in the rest-client. The content-type of the API is "image/jpg" But, in Node.js, the response of…
Dinesh Madanlal
  • 337
  • 4
  • 20
12
votes
1 answer

node.js modify file data stream?

I need to copy one large data file to another destination with some modifications. fs.readFile and fs.writeFile are very slow. I need to read line by line, modify and write to new file. I found something like this: fs.stat(sourceFile, function(err,…
Arti
  • 7,356
  • 12
  • 57
  • 122
12
votes
3 answers

How to delete local file with fs.unlink?

CODE: fs.unlink("/public/images/uploads/"+req.file.filename, (err) => { if (err) { console.log("failed to delete local image:"+err); } else { console.log('successfully deleted local image'); …
Coder1000
  • 4,071
  • 9
  • 35
  • 84
12
votes
1 answer

Open local file in electron and render in wavesurfer.js

I'm working on an app built with electron, it should work with wavesurfer.js to display a waveform representing the audio file. However, I'm having trouble opening the file using the fs module and pushing the file content to wavesurfer via a Blob.…
mspae
  • 276
  • 4
  • 13
12
votes
1 answer

Read/Writing files in node with fs via UNC path

Having issues of reading/writing to a UNC path with nodeJs on my local machine. At one point fs was read/writing from my machine to the UNC path just fine, but now it appears that it cannot read/write to it. UNC Path : \\[machine…
afreeland
  • 3,889
  • 3
  • 31
  • 42
12
votes
3 answers

Move all files in directory to parent with node.js

Question Is there a simple way to move all the files in a directory up to its parent directory then delete the directory? Use Case I'm doing a zip extraction and the source zip contains a root folder called archive, so when I extract I get…
Ralph Callaway
  • 1,768
  • 1
  • 15
  • 34