Code Monkey home page Code Monkey logo

node-fs-extra's Introduction

Node.js: fs-extra

fs-extra adds file system methods that aren't included in the native fs module and adds promise support to the fs methods. It also uses graceful-fs to prevent EMFILE errors. It should be a drop in replacement for fs.

npm Package License build status downloads per month JavaScript Style Guide

Why?

I got tired of including mkdirp, rimraf, and ncp in most of my projects.

Installation

npm install fs-extra

Usage

CommonJS

fs-extra is a drop in replacement for native fs. All methods in fs are attached to fs-extra. All fs methods return promises if the callback isn't passed.

You don't ever need to include the original fs module again:

const fs = require('fs') // this is no longer necessary

you can now do this:

const fs = require('fs-extra')

or if you prefer to make it clear that you're using fs-extra and not fs, you may want to name your fs variable fse like so:

const fse = require('fs-extra')

you can also keep both, but it's redundant:

const fs = require('fs')
const fse = require('fs-extra')

ESM

There is also an fs-extra/esm import, that supports both default and named exports. However, note that fs methods are not included in fs-extra/esm; you still need to import fs and/or fs/promises seperately:

import { readFileSync } from 'fs'
import { readFile } from 'fs/promises'
import { outputFile, outputFileSync } from 'fs-extra/esm'

Default exports are supported:

import fs from 'fs'
import fse from 'fs-extra/esm'
// fse.readFileSync is not a function; must use fs.readFileSync

but you probably want to just use regular fs-extra instead of fs-extra/esm for default exports:

import fs from 'fs-extra'
// both fs and fs-extra methods are defined

Sync vs Async vs Async/Await

Most methods are async by default. All async methods will return a promise if the callback isn't passed.

Sync methods on the other hand will throw if an error occurs.

Also Async/Await will throw an error if one occurs.

Example:

const fs = require('fs-extra')

// Async with promises:
fs.copy('/tmp/myfile', '/tmp/mynewfile')
  .then(() => console.log('success!'))
  .catch(err => console.error(err))

// Async with callbacks:
fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
  if (err) return console.error(err)
  console.log('success!')
})

// Sync:
try {
  fs.copySync('/tmp/myfile', '/tmp/mynewfile')
  console.log('success!')
} catch (err) {
  console.error(err)
}

// Async/Await:
async function copyFiles () {
  try {
    await fs.copy('/tmp/myfile', '/tmp/mynewfile')
    console.log('success!')
  } catch (err) {
    console.error(err)
  }
}

copyFiles()

Methods

Async

Sync

NOTE: You can still use the native Node.js methods. They are promisified and copied over to fs-extra. See notes on fs.read(), fs.write(), & fs.writev()

What happened to walk() and walkSync()?

They were removed from fs-extra in v2.0.0. If you need the functionality, walk and walkSync are available as separate packages, klaw and klaw-sync.

Third Party

CLI

fse-cli allows you to run fs-extra from a console or from npm scripts.

TypeScript

If you like TypeScript, you can use fs-extra with it: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/fs-extra

File / Directory Watching

If you want to watch for changes to files or directories, then you should use chokidar.

Obtain Filesystem (Devices, Partitions) Information

fs-filesystem allows you to read the state of the filesystem of the host on which it is run. It returns information about both the devices and the partitions (volumes) of the system.

Misc.

Hacking on fs-extra

Wanna hack on fs-extra? Great! Your help is needed! fs-extra is one of the most depended upon Node.js packages. This project uses JavaScript Standard Style - if the name or style choices bother you, you're gonna have to get over it :) If standard is good enough for npm, it's good enough for fs-extra.

js-standard-style

What's needed?

  • First, take a look at existing issues. Those are probably going to be where the priority lies.
  • More tests for edge cases. Specifically on different platforms. There can never be enough tests.
  • Improve test coverage.

Note: If you make any big changes, you should definitely file an issue for discussion first.

Running the Test Suite

fs-extra contains hundreds of tests.

  • npm run lint: runs the linter (standard)
  • npm run unit: runs the unit tests
  • npm run unit-esm: runs tests for fs-extra/esm exports
  • npm test: runs the linter and all tests

When running unit tests, set the environment variable CROSS_DEVICE_PATH to the absolute path of an empty directory on another device (like a thumb drive) to enable cross-device move tests.

Windows

If you run the tests on the Windows and receive a lot of symbolic link EPERM permission errors, it's because on Windows you need elevated privilege to create symbolic links. You can add this to your Windows's account by following the instructions here: http://superuser.com/questions/104845/permission-to-make-symbolic-links-in-windows-7 However, I didn't have much luck doing this.

Since I develop on Mac OS X, I use VMWare Fusion for Windows testing. I create a shared folder that I map to a drive on Windows. I open the Node.js command prompt and run as Administrator. I then map the network drive running the following command:

net use z: "\\vmware-host\Shared Folders"

I can then navigate to my fs-extra directory and run the tests.

Naming

I put a lot of thought into the naming of these functions. Inspired by @coolaj86's request. So he deserves much of the credit for raising the issue. See discussion(s) here:

First, I believe that in as many cases as possible, the Node.js naming schemes should be chosen. However, there are problems with the Node.js own naming schemes.

For example, fs.readFile() and fs.readdir(): the F is capitalized in File and the d is not capitalized in dir. Perhaps a bit pedantic, but they should still be consistent. Also, Node.js has chosen a lot of POSIX naming schemes, which I believe is great. See: fs.mkdir(), fs.rmdir(), fs.chown(), etc.

We have a dilemma though. How do you consistently name methods that perform the following POSIX commands: cp, cp -r, mkdir -p, and rm -rf?

My perspective: when in doubt, err on the side of simplicity. A directory is just a hierarchical grouping of directories and files. Consider that for a moment. So when you want to copy it or remove it, in most cases you'll want to copy or remove all of its contents. When you want to create a directory, if the directory that it's suppose to be contained in does not exist, then in most cases you'll want to create that too.

So, if you want to remove a file or a directory regardless of whether it has contents, just call fs.remove(path). If you want to copy a file or a directory whether it has contents, just call fs.copy(source, destination). If you want to create a directory regardless of whether its parent directories exist, just call fs.mkdirs(path) or fs.mkdirp(path).

Credit

fs-extra wouldn't be possible without using the modules from the following authors:

License

Licensed under MIT

Copyright (c) 2011-2024 JP Richardson

node-fs-extra's People

Contributors

abetomo avatar ackar avatar agnivade avatar atao60 avatar bartland avatar binki avatar hhamilto avatar jpeer264 avatar jprichardson avatar lamweili avatar manidlou avatar mbargiel avatar mchunkytrunk avatar mikermcneil avatar nacd avatar newmanw avatar overlookmotel avatar peterdavehello avatar philippefutureboy avatar prayagverma avatar ramhejazi avatar raybellis avatar reggi avatar reisclef avatar revelt avatar rjz avatar ryanzim avatar sri-rang avatar sukkaw avatar ulikoehler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-fs-extra's Issues

fs.remove yields callback before directory is really deleted

fs = require "fs.extra"

dir = "test"

fs.mkdirSync dir

fs.delete dir, ->
    fs.mkdir dir
    ###
    Error: EEXIST, file already exists 'test'
        at Object.fs.mkdirSync (fs.js:483:18)
        at Object.<anonymous> (/Users/vyacheslav/test.coffee:8:6)
        at Object.<anonymous> (/Users/vyacheslav/test.coffee:14:4)
        at Module._compile (module.js:449:26)
        at Object.exports.run (/usr/local/share/npm/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:82:25)
        at compileScript (/usr/local/share/npm/lib/node_modules/coffee-script/lib/coffee-script/command.js:177:29)
        at fs.stat.notSources.(anonymous function) (/usr/local/share/npm/lib/node_modules/coffee-script/lib/coffee-script/command.js:152:18)
        at fs.readFile (fs.js:176:14)
        at Object.oncomplete (fs.js:297:15)
    ###

Stack trace not included in fs.copy error

To reproduce:

var fs = require('fs-extra');
fs.copy('/does/not/exist', '/something/else', function(err) {
    console.log('error:', err); // error is nested in array
    console.log('error stack:', err[0].stack); // message prints but no stack trace
});
  1. Why the array nesting? Is it necessary?
  2. Stack trace should be present. Mandatory for debugging.

don't import normal fs stuff into fs-extra

When reading code, if I come across fs.mkdir, I don't expect that to have mkdir -p behavior. Code readers should be able to depend on knowing that fs is node.js's api unaltered. Better readability would look like this:

fs = require('fs')
fse = require('fs-extra')

...and then use fs for node's native api and fse for your extra functionality. This is the best way to write code. You should encourage your users to write code this way by not also exporting fs's functions in fs-extra.

Add ensure methods

"ensure" methods would ensure some type of behavior. For example ensureDir(dir) would ensure that the dir exists. Or even better, ensurePath(path) would ensure that the file or directory exists. ensureDelete(path), ensureGone(path) or something similarly named would ensure that the path is deleted or does not exist.

Maybe these names:

  • ensurePathExists(path)
  • ensurePathNotExists(path)

but those feel a bit verbose.

Any ideas?

touch() in 0.10.0

Hey guys, I saw the warning about touch being deprecated, but I wanted to mention that in node 0.10.0 it is hanging- might want to change the deprecation warning to an error.

Thanks for the great module!

fs.copy issue

From a user:

I’m having an issue with .copy(), the dest file is ending up with root as the owner, so git is freaking out that it doesn’t have permissions to reset or remove the file.

file-extra npm !exist

readme says: npm install file-extra

there is no file-extra in npm. i think it should be fs-extra??

npm install fs-extra doesn't work

Hi, I have tried to reinstall all modules. And now i can't install properly fs-extra. Sometimes console says it can't find compatible ncp, module, i try to install ncp, then it start to say that it can't find compatible mkdirp module, then i install mkdirp, and it says about ncp again.

D:\work\WebServers\home\localhost\www\ThemeFramework\ThemeFramework>npm install
fs-extra
npm http GET https://registry.npmjs.org/fs-extra
npm http 304 https://registry.npmjs.org/fs-extra
npm http GET https://registry.npmjs.org/ncp
npm http GET https://registry.npmjs.org/jsonfile
npm http GET https://registry.npmjs.org/rimraf
npm ERR! Error: No compatible version found: mkdirp@'^0.5.0'
npm ERR! Valid install targets:
npm ERR! ["0.0.1","0.0.2","0.0.3","0.0.4","0.0.5","0.0.6","0.0.7","0.1.0","0.2.0
","0.2.1","0.2.2","0.3.0","0.3.1","0.3.2","0.3.3","0.3.4","0.3.5","0.4.0","0.4.1
","0.4.2","0.5.0"]
npm ERR! at installTargetsError (C:\Program Files\nodejs\node_modules\npm\li
b\cache.js:709:10)
npm ERR! at C:\Program Files\nodejs\node_modules\npm\lib\cache.js:631:10
npm ERR! at RegClient.get_ (C:\Program Files\nodejs\node_modules\npm\node_mo
dules\npm-registry-client\lib\get.js:101:14)
npm ERR! at RegClient. (C:\Program Files\nodejs\node_modules\npm
node_modules\npm-registry-client\lib\get.js:37:12)
npm ERR! at fs.js:266:14
npm ERR! at Object.oncomplete (fs.js:107:15)
npm ERR! If you need help, you may report this log at:
npm ERR! http://github.com/isaacs/npm/issues
npm ERR! or email it to:
npm ERR! [email protected]

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\Program Files\nodejs\node.exe" "C:\Program Files\nod
ejs\node_modules\npm\bin\npm-cli.js" "install" "fs-extra"
npm ERR! cwd D:\work\WebServers\home\localhost\www\ThemeFramework\ThemeFramework

npm ERR! node -v v0.10.7
npm ERR! npm -v 1.2.21
npm http 304 https://registry.npmjs.org/ncp
npm http 304 https://registry.npmjs.org/jsonfile
npm http 304 https://registry.npmjs.org/rimraf
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! D:\work\WebServers\home\localhost\www\ThemeFramework\ThemeFramework
\npm-debug.log
npm ERR! not ok code 0

Copy failing if dest directory doesn't exist. Is this intended?

I'm getting following error when trying to copy files. It only seems to happen if the file needs to go into a directory that doesn't exist.

Is this supported behavior?

Here's what I'm doing:

fsExtra.copy('page.html', 'foo/bar/page.html')

And this is the error I get:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: ENOENT, open '/Users/philip/Projects/torpedo/foo/bar/page.html'

Bug in copy - callback called on readStream "close" - Fixed in ncp 0.5.0

The bug is in the ncp version used. It is fixed in 0.5.0.
copy callback called when the readStream "close" event is handled. Therefore, the writeStream is not closed when the callback is called. For example, this can (race condition) create EPERM errors if you try to rename the destination file once the copy is done.

version bump to 0.2

It seems that you broke api compatibility between 0.1.0 and 0.1.2.

Conventionally that would warrant a bump in the minor version for a pre-release product so that others can depend on 0.1.x to add features but not remove them.

I don't know if that was intentional or not, but I think it would be great if you published a 0.1.3 that has backwards compatibility and then published a 0.2 without the backwards compatibility.

=|8^D

Performance issue?

I'm using fs.copy on a local SSD and copying a ~500MB file, it seems to take at least 4-5 full seconds to copy. Any idea why it would take so long? It is copying over to a directory that is about 5 levels deep.

fs.copy throw unexplained error ENOENT, utime

Hi

trying to copy a file from path a to another path 50% of the time, I get the following error
Error: ENOENT, utime 'my target path'
this happen right after I call fs.copy(oldPath, targetpath, function (error) { - -it get to the block with the above error - any idea ?

the file exist in the old path, but the strange thing is that it is also appeared on the target path , so the copy worked ???

what is the above error ? (what is the utime part of it) ?

I think it happens when fs.copy try to set the timestamp of the file after copying it, when I wrap it with try catch it never happened again - but the problem is that its not consistent it happen only in 50% of the times.

thanks

chmod & chown for mkdirs

So I create a bunch of directories with mkdirs and now I need to change permissions and ownership for the created directories.

There should be an optional options argument to mkdirs:

{
  chmod: 400,
  chown: {
    user: "user",
    group: "group"
  }
}

Thoughts on above or how I would solve this otherwise?

fs.mkdirSync is broken in 0.3.1

This was working in 0.2.1. For example, when I try to create foo/bar/baz/blah, if foo/bar/ already exist then it should skip those two and should continue creating all the children folders, it should not throw any exception (at least it wasn't doing that in 0.2.1).

comments on naming

  • mkdir - I believe that fs-extra.mkdir should not have different behavior than fs.mkdir.

Crash when have no access to write to destination file in copy

When I try to copy file and I have no access to write to distillation, my application is crashing with error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: EACCES, open '/home/io/name.js'

There is example of code I use:

var fse  = require('fs-extra');

fse.copy(from, to, function(error) {
    console.log(error);
});

Looks like there is a problem in ncp module. But author staying quite. Maybe ncp could be changed with another module which do the same, but more correct?

ENAMETOOLONG

I'm getting ENAMETOOLONG on trying to create this file via fsExtra.mkdirs:
ENAMETOOLONG
/Users/flaberge/dev/topperharley/work/507017973001/587bcadc-b170-41f3-bc3e-656be3fe5031/default

But using mkdir -p /Users/flaberge/dev/topperharley/work/507017973001/587bcadc-b170-41f3-bc3e-656be3fe5031/default via command line does work.

On the latest MacbookPro, on Yosemite. This looks to be coming from here or near in libuv, but I figured you might be familiar with the issue and more responsive.

fs.copy err is empty array

I'm using a mix of yeoman functions and node code here so even though the yeoman functions run synchronously by default, I don't know if that means this fs.copy function will wait for them to run. So the issue may be that the file doesn't exist yet but I'm not getting an error from fs.copy().

//yeoman function copies all files from template to working directory
this.directory('.','.', true);

var fs = require('fs-extra');

var source = 'path/to/fileA';
var destination = 'path/to/fileB';

fs.copy(source, destination, function(err){
  if (err) {
    console.error(err);
  }
  else {
    console.log("success!")
  }
});

I can confirm all the files are copied by yeoman and exist. Then I want to move one file in particular from the ones yeoman copied over. I can use yeoman's built in generator.copy() method as below and it works. But, I need something I can run synchronously with fs.unlink() to delete the source file after moving it. That is why I am trying to use your fs.copy() method. I can use a callback with it while neither of the two yeoman functions allows a callback and neither of them returns true/false. They just return an instance of the generator (for what reason I have no clue).

//yeoman version of copy
this.copy(source, destination);

This leads me to believe it is because fs.copy() is running before yeoman gets the files copied over so I guess I would expect to see an error like 'can't find file' or 'file does not exist' but I get '[]' for the error.

So the issue is getting some type of error from fs.copy() but if you can tell me how to make it run synchronously with the yeoman functions that would be great too. But I'm guessing that is something I need to take up with them.

Update Readme

Change things like...

fs.copy('/tmp/myfile', '/tmp/mynewfile', function(err){
  if (err) {
    console.error(err);
  }
  else {
    console.log("success!")
  }
}); //copies file

to

fs.copy('/tmp/myfile', '/tmp/mynewfile', function(err) {
  if (err) return console.error(err)
  console.log('success')    
})

[bug] cannot run in strict mode

hello there

just tried to run my app in strict mode and found this error in the console:

.../node_modules/fs-extra/lib/create.js:14
      function makeFile() {
      ^^^^^^^^
SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function.

do you think you could refactor this to make it work for strict mode?

thanks

Better error reporting

When supplying a wrong directory name, the copy function returns an undefined error:

fs.copy( '/non-existing-directory', '/platforms/android/res', function (err) {
    if (err) return console.error('An error occured! '+ err); //  err = undifined
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.