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
33
votes
4 answers

React Js require 'fs'

I have import fs from 'fs' and in my package.json I have Then I run the command > npm i fs > fs@0.0.2 node_modules/fs next in my React store I import 'fs' module import fs from 'fs' However when I try to use fs I don't see methods…
joe
  • 425
  • 1
  • 5
  • 9
33
votes
3 answers

How to read the content of files synchronously in Node.js?

This is what I have: #! /usr/bin/env node var fs = require('fs'), files = fs.readdirSync(__dirname + '/files/'), files.forEach(function(file) { fs.readFile(__dirname + '/files/' + file, 'utf8', function (error, data) { console.log(data) …
alexchenco
  • 53,565
  • 76
  • 241
  • 413
29
votes
3 answers

Cannot find file in Node even though it has correct path

After executing this code: const filename = "../../.dburl" const url = fs.readFileSync(filename, 'utf-8') I recieve the following error: Error: ENOENT: no such file or directory, open '../../.dburl' What I know so far: 1) The filepath is…
srpalo
  • 493
  • 1
  • 5
  • 12
29
votes
1 answer

Webpack nodejs fs.readFile is not a function

I have a webpack config like: var path = require('path') module.exports = { entry: "./index.js", output: { path: path.join(__dirname, 'static'), filename:'bundle.js' }, module: { loaders: [ {…
gamer
  • 5,673
  • 13
  • 58
  • 91
28
votes
3 answers

how to determine whether the directory is empty directory with nodejs

I have searched the Nodejs Doc,But don't find relative API. So I write the following code to determine whether the directory is empty directory. var fs = require('fs'); function isEmptyDir(dirnane){ try{ fs.rmdirSync(dirname) } …
kwoktung
  • 572
  • 1
  • 4
  • 12
28
votes
3 answers

change a file using node.js

I have a text file that looks like…
Connor Leech
  • 18,052
  • 30
  • 105
  • 150
27
votes
5 answers

How to disable warnings when node is launched via a (global) shell script

I am building a CLI tool with node, and want to use the fs.promise API. However, when the app is launched, there's always an ExperimentalWarning, which is super annoying and messes up with the interaction prompts. How can I disable this warning/all…
Blazing Fast
  • 703
  • 1
  • 7
  • 12
27
votes
1 answer

process.env.PWD vs process.cwd()

I am using Meteor JS...and within my Meteor app I am using node to query the contents of different directories within the app.... When I use process.env.PWD to query the contents of a folder I get a different result from when I use process.cwd() to…
preston
  • 3,721
  • 6
  • 46
  • 78
26
votes
2 answers

Mock fs function with jest

First of all, I'm new to es6 and jest. I have a Logger class for instantiate winston and I would like to test it. Here my code : const winston = require('winston'); const fs = require('fs'); const path = require('path'); const config =…
Oyabi
  • 824
  • 3
  • 10
  • 27
25
votes
4 answers

NodeJs: fs.stat() or fs.access() to check if a folder exists?

I am currently trying to figure out the "correct" way to check if a local folder exists, before saving a file into it and am a little bit confused by the nodejs docs. fs.exists() is deprecated and you should use fs.stat() or fs.access(). So far so…
BenSower
  • 1,532
  • 1
  • 13
  • 26
24
votes
5 answers

Not works fs.readFile in node js

I have: fs.readFile('../services/Prescipcion.xml', "utf8", function (err, data) { console.log("err->", err); console.log("data", data); }); And it logs: err-> { [Error: ENOENT: no such file or directory, open…
oihi08
  • 737
  • 1
  • 6
  • 20
23
votes
4 answers

How to pipe multiple readable streams, from multiple api requests, to a single writeable stream?

- Desired Behaviour - Actual Behaviour - What I've Tried - Steps To Reproduce - Research Desired Behaviour Pipe multiple readable streams, received from multiple api requests, to a single writeable stream. The api responses are from…
user1063287
  • 10,265
  • 25
  • 122
  • 218
23
votes
1 answer

Mocking file input in React TestUtils

I have a component with the following render function:- render: function() {
fbielejec
  • 3,492
  • 4
  • 27
  • 35
23
votes
1 answer

fs.statSync throws error if file does not exist

I am attempting to determine if a file exists. If it does not exist, I would like my code to continue so it will be created. When I use the following code, if the file exists, it prints that 'it exists'. If it does not exist, it crashes my app. Here…
user5096599
23
votes
2 answers

Node.js: Check if file is an symbolic link when iterating over directory with 'fs'

Supervisor is a package for Node.js that monitors files in your app directory for modifications and reloads the app when a modification occurs. This script interprets symbolic links as regular files and logs out a warning. I would like to fork…
james_womack
  • 10,028
  • 6
  • 55
  • 74