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.
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.
Share Improve this question edited Feb 3, 2012 at 21:44 Brandon asked Feb 3, 2012 at 20:16 BrandonBrandon 1,4094 gold badges21 silver badges37 bronze badges 7 | Show 2 more comments4 Answers
Reset to default 3My 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);
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.
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.
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.
-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 Commented Feb 3, 2012 at 21:13