最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS fs.open failing on existing file (not a path issue) - Stack Overflow

programmeradmin1浏览0评论

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
  • 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 Commented Feb 3, 2012 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 Commented Feb 3, 2012 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 Commented Feb 3, 2012 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. github.com/joyent/node/issues/search?q=ENOENT – loganfsmyth Commented Feb 4, 2012 at 1:45
  • @loganfsmyth Yes, I'm running Node 0.6.7 on OS X Lion. I should have added that to the question. – Brandon Commented Feb 4, 2012 at 3:42
 |  Show 2 more comments

4 Answers 4

Reset to default 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);

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.

发布评论

评论列表(0)

  1. 暂无评论