7

I'm new to Node.js and I'm trying to figure out what is wrong with the following code.

var fs = require('fs');
var dir = "C:\\";
var files = fs.readdirSync(dir);
for (var i = 0; i < files.length; i++) {
    var name = fs.statSync(dir + files[i]).name;
}

When running this code I get the error:

Error: EBUSY, resource busy or locked 'C:\hiberfil.sys'
    at Object.statSync (fs.js:424:18)
    at Object.<anonymous> (S:\start.js:7:19)
    at Module._compile (module.js:446:26)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)
    at Function._load (module.js:311:12)
    at Array.0 (module.js:484:10)
    at EventEmitter._tickCallback (node.js:190:38)
tshepang
  • 12,111
  • 21
  • 91
  • 136
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
  • Try this first https://stackoverflow.com/questions/36566236/npm-install-error-code-ebusy-errono-4082/45757541#45757541 – sijo vijayan Aug 18 '17 at 13:13

2 Answers2

11

Hiberfil.sys is a system file that holds the hibernation data onto disk, you cannot just read it via normal rights, it's super confidential since it hold all memory information on disk.

Mustafa
  • 10,013
  • 10
  • 70
  • 116
  • 1
    Just tossing a note in here for reference: Using a try/catch fixed my problem with this error message. `try { var name = fs.statSync(path); }` -- it just ignores it if the file cannot be read for whatever reason, and moves on. Node has really helped me understand the value of error handling in JS. – Adrian Dec 04 '13 at 05:16
  • 1
    @Adrian can you pls make a full example of you solution thanks in advance – haltman Jan 18 '19 at 12:00
  • @haltman I posted this more than five years ago. I don't remember the context for my comment nor the discovery that led to it, plus I have not used JavaScript or Node very much in the past two years. I think my point was that using try/catch when trying to read a directory helped avoid pointless errors like trying to read a protected system file. Wrap the fs.statSync call in a try block, and catch any errors it throws rather than allowing them to crash your app – Adrian Feb 01 '19 at 00:45
1

After having a similar issue without finding a sufficient answer:

I have fixed it by searching for circular dependencies.

After fixing it, I was able to run everything normally. enter image description here

matan yemini
  • 141
  • 2
  • 4