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!
You are almost there.
There are two Node JS File System operations that can open a FileHandle (or file pointer).
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.
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);
}
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