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

node.js - fs.open() won't open the file - Stack Overflow

programmeradmin28浏览0评论

Running Node.js v22.12.0.

Trying hard to open the file:

  const fs = require("fs");

   const file = await fs.open(
  "IP2LOCATION-COUNTRY-REGION-CITY.CSV",
  "r",
  (err) => {
    console.log("Err is " + err);
  }
);

console.log(file);

The file EXISTS in the project dir

-rwxrwxrwx   1 ubuntu ubuntu 12273675 Dec 31 16:00 IP2LOCATION-COUNTRY-REGION-CITY.CSV

Results:

File is undefined
Err is null

If I try to make a deliberate error specifying file name in the code, then results are:

File is undefined
Err is Error: ENOENT: no such file or directory, open 'IP2LOCATION-COUNTRY-REGION-CIT.CSV'

This proves the file is there.

Really really puzzled. The actuall file is non-empty CSV file, 12 MB in size.

Any ideas very appreciated!

Running Node.js v22.12.0.

Trying hard to open the file:

  const fs = require("fs");

   const file = await fs.open(
  "IP2LOCATION-COUNTRY-REGION-CITY.CSV",
  "r",
  (err) => {
    console.log("Err is " + err);
  }
);

console.log(file);

The file EXISTS in the project dir

-rwxrwxrwx   1 ubuntu ubuntu 12273675 Dec 31 16:00 IP2LOCATION-COUNTRY-REGION-CITY.CSV

Results:

File is undefined
Err is null

If I try to make a deliberate error specifying file name in the code, then results are:

File is undefined
Err is Error: ENOENT: no such file or directory, open 'IP2LOCATION-COUNTRY-REGION-CIT.CSV'

This proves the file is there.

Really really puzzled. The actuall file is non-empty CSV file, 12 MB in size.

Any ideas very appreciated!

Share Improve this question asked Jan 17 at 13:38 jurijus01jurijus01 375 bronze badges 9
  • There is a Y missing in "city" in the error. Is there a typo in your code? – Sean Commented Jan 17 at 13:47
  • 1 You're mixing a callback with await. Don't. The API is going to only be promise-based or callback-based. Not both. If you pass a callback, then the resulting file will be in the callback. – VLAZ Commented Jan 17 at 13:49
  • It crashes without callback. It used to work fine before. Is there way to read file async? – jurijus01 Commented Jan 17 at 14:00
  • 1 If you're using the callback-based API then a callback is needed. Otherwise you can't work with the result. It will very well throw an error if you use the API wrong, therefore being unable to work with it. That makes perfect sense. The solution is to use the API correctly. Rather than make up a hodge-podge. If you want the promise-based API - then use that. If you are using the callback-based API - then supply a correct callback. You can't have both. – VLAZ Commented Jan 17 at 14:02
  • But it used to work. – jurijus01 Commented Jan 17 at 14:07
 |  Show 4 more comments

1 Answer 1

Reset to default 3

You are almost there.

There are two Node JS File System operations that can open a FileHandle (or file pointer).

  • fs.open
  • fsPromises.open

One valid way to open a file pointer is using fs.open.

const fs = require("fs");
fs.open("IP2LOCATION-COUNTRY-REGION-CITY.CSV", "r", (err, fp) => {
    if (err) console.log("error", err);
    else console.log("file pointer", fp);
});

Anothe valid way to open a file pointer is using fsPromises.open.

const fs = require("fs").promises;
try {
    const fp = await fs.open("IP2LOCATION-COUNTRY-REGION-CITY.CSV", "r");
    console.log("file pointer", fp);
} catch (err) {
    console.log("error", err);
}

But maybe more suited for you is to read the content of a file directly instead of opening a filepointer.

  • fs.readFile.
const fs = require("fs").promises;
try {
    const csvContent = await fs.readFile(`IP2LOCATION-COUNTRY-REGION-CITY.CSV`, { encoding: "utf8" });
    console.log(csvContent);
} catch (err) {
    console.error(err);
}
发布评论

评论列表(0)

  1. 暂无评论