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
11
votes
5 answers

Module not found: Error: Can't resolve 'fs' in dotenv/lib

All of a sudden, when creating a react production build, I get this error. > safe-courier@0.1.0 build > react-scripts build Creating an optimized production build... Failed to compile. Module not found: Error: Can't resolve 'fs' in…
Kally
  • 324
  • 1
  • 4
  • 12
11
votes
5 answers

How can I use fs.createReadstream with fs.promises

I use require("fs").promises just to avoid to use callback function. But now, I also want to use fs.createReadstream to attach a file with POST request. How can I do this? Or what alter createReadstream in this case? Or should I use require("fs")?
invalid
  • 1,215
  • 4
  • 13
  • 30
11
votes
3 answers

Stream response to file using Fetch API and fs.createWriteStream

I'm creating an Electron application and I want to stream an image to a file (so basically download it). I want to use the native Fetch API because the request module would be a big overhead. But there is no pipe method on the response, so I can't…
Tamás
  • 950
  • 2
  • 10
  • 29
11
votes
1 answer

How to create a readstream with a buffer using Node.js

I have this json: var myJSON = '{"kind": "person", "fullName": "Rivka3"}'; I'm trying to uploed it to bigquery, using createReadStream. when I save it localy I succeed: fs.writeFile("/tmp/bq_json_file_new.json", myJSON,…
dina
  • 4,039
  • 6
  • 39
  • 67
10
votes
3 answers

How to Mock `fs.promises.writeFile` with Jest

I am trying to mock the promise version of fs.writeFile using Jest, and the mocked function is not being called. Function to be tested (createFile.js): const { writeFile } = require("fs").promises; const createNewFile = async () => { await…
Dan Levy
  • 3,931
  • 4
  • 28
  • 48
10
votes
5 answers

Accessing filesystem in Angular 2 app using Electron

I know that Angular 2 is run on a web browser, which does not have access to the file system. However, I'm using Electron as my front-end, and also running the app via electron: "build-electron": "ng build --base-href . && cp src/electron/*…
Kevin
  • 979
  • 4
  • 10
  • 22
10
votes
3 answers

fs.readFileSync is not a function Meteor, React

I'm getting a 'fs.readFileSync is not a function' in Chrome debugger after trying to call readFileSync(); I call it... const fs = require('fs'); call the function... let content = fs.readFileSync('/path/to/my/file.stuff'); And attempt to display…
VocoJax
  • 1,469
  • 1
  • 13
  • 19
10
votes
4 answers

Can Multiple fs.write to append to the same file guarantee the order of execution?

Assume we have such a program: // imagine the string1 to string1000 are very long strings, which will take a while to be written to file system var arr = ["string1",...,"string1000"]; for (let i = 1; i < 1000; i++) { …
Lubor
  • 989
  • 3
  • 10
  • 33
10
votes
1 answer

How to reset nodejs stream?

How to reset nodejs stream? How to read stream again in nodejs? Thanks in advance! var fs = require('fs'); var lineReader = require('line-reader'); // proxy.txt = only 3 lines var readStream =…
Игорь
  • 158
  • 1
  • 2
  • 11
10
votes
2 answers

Find String in a Txt File, Delete Entire Line

I'm currently working with node.js to create an IRC bot. The bot allows users to add song links to a database. Each time someone submits a song, it is added to a new line of "shuffle.txt" as such: user1,The Beatles,Yesterday,(youtube link) user2,The…
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
10
votes
1 answer

Node.js: console.log message doesn't show up if method throws exception... why?

In Node.js, if I have a method that throws an exception, console.log statements from that method don't fire. I recognize that in the simple test case below that I should catch the exception from the readFileSync call, or otherwise be defensive about…
Shaun
  • 625
  • 1
  • 6
  • 17
10
votes
4 answers

Saving an image stored on s3 using node.js?

I'm trying to write an image server that uses node.js to store images on s3. Uploading the image works fine, and I can download and view it correctly using an s3 browser client (I'm using dragondisk, specifically, but I've successfully downloaded it…
tandrewnichols
  • 3,456
  • 1
  • 28
  • 33
10
votes
1 answer

Read file with fs.readFileSync and eval contents...which scope have the functions? How to access?

I recently tried to import a file into my existing node.js project. I know this should be written with a module but i include my external javascript file like this: eval(fs.readFileSync('public/templates/simple.js')+'') The contents of simple.js…
Johnnycube
  • 1,060
  • 1
  • 10
  • 25
9
votes
1 answer

How to convert ReadableStream into ReadStream in NodeJS?

I'm using nodemailer, and pdfmake to create a pdf, and send it in the email's attachments. I don't have too much experience with file handling in NodeJS, and I can't make it work. See example where the file is saved. As I've checked the types,…
Gergő Horváth
  • 3,195
  • 4
  • 28
  • 64
9
votes
2 answers

fs.existsSync is not a function when used in electron

I am using Angular 10, Electron 10.0 and electron-builder v22.8.0. When starting my Electron app, I receive the following error in the console: fs.existsSync is not a function when used in electron getElectronPath @…
Daniel Stephens
  • 2,371
  • 8
  • 34
  • 86