14

I've been dealing with this for a long time, so any help is much appreciated. So, I'm downloading a file and saving it using PhantomJS and CasperJS. Let me point out that they aren't the issue. The file is downloaded without a problem.

The problem is that NodeJS won't recognize or open the file after it is downloaded. I can't fs.stat, fs.open, etc.. Nothing works.

I'll share the code in a second, but here's the log:

Here: bdTcK6hSdownload.csv

[ '2puzZMeLdownload.csv',
  '2s5ICbKNdownload.csv',
  'bdTcK6hSdownload.csv',
  'izIfagwCdownload.csv' ]

fs.js:230

return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);

Error: ENOENT, no such file or directory './caspertemp/bdTcK6hSdownload.csv'

at Object.openSync (fs.js:230:18)
at Object.processCSV (/Users/Home/dev/node_modules/inviter/index.js:64:29)
at /Users/Home/dev/node_modules/inviter/index.js:36:33
at ChildProcess.exithandler (child_process.js:281:7)
at ChildProcess.emit (events.js:70:17)
at maybeExit (child_process.js:361:16)
at Process.onexit (child_process.js:397:5)

As you can see, I'm printing out the created file name, then printing the contents of the directory and then trying to open the file. As you can see, bdTcK6hSdownload.csv exists in the directory but fails on open.

The simple code snippet is here:

console.log('Here: ' + filename);

filenames = fs.readdirSync('./caspertemp/');
console.log(filenames);

var fd = fs.openSync('./caspertemp/' + filename, 'r');
console.log(fd);

There's a bunch more going on before and after this but none of it matters since this basic function fails. Please help! This has been nagging for weeks.

Brandon
  • 1,399
  • 4
  • 20
  • 37
  • 1
    Hmm. Have you checked permissions? Also, run node.js like this: strace ./node - this will trace all the system calls and you are looking for an open(), check the path node is using, etc. This will tell you what the node process is actually doing. – EdH Feb 03 '12 at 20:42
  • @EdH. For permissions yes. All files have the same permissions: `-rw-r--r-- 1 Home staff 216102 Feb 3 15:00 bdTcK6hSdownload.csv` It's also worth noting that if I restart node I can access the file without a problem. – Brandon Feb 03 '12 at 21:13
  • @EdH.Hmm, I can't run in strace mode for some reason: mac:dev Home$ strace -fF -o strace.log node app.js -bash: strace: command not found - Not sure what I'm doing wrong with that command – Brandon Feb 03 '12 at 21:48
  • Are you running a recent version of node? Have you checked the issue queue? There are a bunch of Windows issues for ENOENT, though most are closed now. https://github.com/joyent/node/issues/search?q=ENOENT – loganfsmyth Feb 04 '12 at 01:45
  • @loganfsmyth Yes, I'm running Node 0.6.7 on OS X Lion. I should have added that to the question. – Brandon Feb 04 '12 at 03:42
  • @Brandon any motion on this? I have the exact same problem as well. node 0.8.8, Lion 10.7.4. The identical code works great on a production Ubuntu server, but behaves as you described in development. – Carson C. Sep 05 '12 at 14:32
  • @CarsonC.No, I ended up bailing on it. Very strange behavior – Brandon Sep 05 '12 at 21:07

4 Answers4

3

My guess is that it's a discrepancy in current working directory. Are you starting the casperJS and node.js processes from the same directory? Do either of them change working directory at runtime? Try something like this, where node's __dirname will give you the directory path of the currently executing .js file

var path = require("path");
var filename = "bdTcK6hSdownload.csv";
var csvPath = path.resolve(path.join(__dirname, "caspertemp", filename));
console.log(csvPath);
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • I appreciate the thoughtful answer and example, but you can see from the code I posted they are returning the same directory. The caspertemp is in root but the working directory is in node_modules. For example, if I use your code and remove the __dirname variable, I'm getting this: /Users/Home/dev/caspertemp/pwAA3Kzzdownload.csv ENOENT, no such file or directory '/Users/Home/dev/caspertemp/pwAA3Kzzdownload.csv – Brandon Feb 04 '12 at 20:39
0

Another possibility is timing.

I've had situations in the past where a file entry appears in the folder straight away but the file is unusable as the thing writing it has either not finished or not yet released it's handle. Under those conditions it isn't unusual to need to either defer processing or to do some other checks to see if things are ready.

On those occasions it may be that a restart 'fixes' things but only because as a side effect it either releases the handle or delays processing, so when your code trys again it's all fine.

K Cartlidge
  • 171
  • 1
  • 6
0

This occurs only when you are giving wrong path. I have come across the same problem.

Initially I was running the file directly node xyz.js and required static files are in same repository. But When I do npm start, code was throwing above error :

    fs.js 495 
    return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
    **Error: ENOENT, no such file or directory**

Then I moved static resources, renamed path and It worked. Hope this may help you.

atul
  • 540
  • 4
  • 11
0

File extension could be entered twice hence not found. Navigate to the file directory with your command prompt and get the list of files in that directory

command:

dir

or

ls

Get the actual file name there and use it. In my case I had saved a text document as "readMe.txt" and on command prompt it appeared as "readMe.txt.txt". I did not have to append the file extension when saving and this created the problem.

Ndheti
  • 266
  • 3
  • 18