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

Read file from aws s3 bucket using node fs

I am attempting to read a file that is in a aws s3 bucket using fs.readFile(file, function (err, contents) { var myLines = contents.Body.toString().split('\n') }) I've been able to download and upload a file using the node aws-sdk, but I am at a…
Joel
  • 1,579
  • 3
  • 12
  • 19
108
votes
17 answers

Module not found: Can't resolve 'fs' in Next.js application

Unable to identify what's happening in my next.js app. As fs is a default file system module of nodejs. It is giving the error of module not found.
Ibad Shaikh
  • 2,704
  • 3
  • 15
  • 27
96
votes
10 answers

writeFile no such file or directory

I have a file(data.file an image), I would like to save this image. Now an image with the same name could exist before it. I would like to overwrite if so or create it if it does not exist since before. I read that the flag "w" should do…
basickarl
  • 37,187
  • 64
  • 214
  • 335
91
votes
4 answers

Write a line into a .txt file with Node.js

I want to use Node.js to create a simple logging system which prints a line before the past line into a .txt file. However, I don't know how the file system functionality from Node.js works. Can someone explain it?
Meterion
  • 913
  • 1
  • 6
  • 5
78
votes
4 answers

What are the pros and cons of fs.createReadStream vs fs.readFile in node.js?

I'm mucking about with node.js and have discovered two ways of reading a file and sending it down the wire, once I've established that it exists and have sent the proper MIME type with writeHead: // read the entire file into memory and then spit it…
Kent Brewster
  • 2,480
  • 2
  • 22
  • 27
75
votes
24 answers

Get all files recursively in directories NodejS

I have a little problem with my function. I would like to get all files in many directories. Currently, I can retrieve the files in the file passed in parameters. I would like to retrieve the html files of each folder in the folder passed as a…
user6285277
57
votes
3 answers

How do I use chmod with Node.js

How do I use chmod with Node.js? There is a method in the package fs, which should do this, but I don't know what it takes as the second argument. fs.chmod(path, mode, [callback]) Asynchronous chmod(2). No arguments other than a possible exception…
pvorb
  • 7,157
  • 7
  • 47
  • 74
44
votes
1 answer

What is the current directory used by fs module functions?

What is the current directory when a method in the fs module is invoked in a typical node.js/Express app? For example: var fs = require('fs'); var data = fs.readFile("abc.jpg", "binary"); What is the default folder used to locate abc.jpg? Is it…
Old Geezer
  • 14,854
  • 31
  • 111
  • 198
44
votes
2 answers

How to refactor Node.js code that uses fs.readFileSync() into using fs.readFile()?

I'm trying to get my head around synchronous versus asynchronous in Node.js, in particular for reading an HTML file. In a request handler, the synchronous version that I'm using, which works is the following: var fs = require("fs"); var…
Bondifrench
  • 1,272
  • 1
  • 20
  • 35
43
votes
14 answers

Node fs Error: EPERM: operation not permitted, open

I get this error in my app: Error: EPERM: operation not permitted, open 'C:\Program Files (x86)\Full Menu\db\main.json' The app I have is built with electron-boilerplate. I am using this function to get the path to the root of the…
Sergiu
  • 441
  • 1
  • 4
  • 7
43
votes
6 answers

Download file from url and upload it to AWS S3 without saving - node.js

I'm writing an application which downloads images from a url and then uploads it to an S3 bucket using the aws-sdk. Perviously I was just downloading images and saving them to disk like this. request.head(url, function(err, res, body){ …
Loourr
  • 4,995
  • 10
  • 44
  • 68
42
votes
7 answers

Find absolute base path of the project directory

Until now we could get the absolute path of a file to open later as readStream with this code snippet: var base = path.resolve('.'); var file = base + '/data/test.csv'; fs.createReadStream(file) Since Meteor 0.6.5 the base path is pointing to…
loomi
  • 2,936
  • 3
  • 25
  • 28
36
votes
4 answers

Uncaught TypeError: URL is not a constructor using WHATWG URL object support for electron

I am trying to read a file using WHATWG URL object support here and I am getting this error: Uncaught TypeError: URL is not a constructor here is my code: var fs = require("fs"); const { URL } =…
Ana Houa
  • 901
  • 1
  • 8
  • 19
34
votes
3 answers

How to read csv file in node js

I am trying to read a csv file using node js. Her is my code fs.readFile(config.csvUploadPath, function read(err, data) { if (err) { throw err; } console.log(data + 'my data') }); CONSOLE: ID D11 D33 D55 Here I want to get the…
MMR
  • 2,869
  • 13
  • 56
  • 110
34
votes
6 answers

Node fs copy a folder

I am trying to copy a folder using Node fs module. I am familiar with readFileSync() and writeFileSync() methods but I am wondering what method I should use to copy a specified folder?
y. lu
  • 341
  • 1
  • 3
  • 4