-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading files with nodeJS typo and fetch failure with example link #7510
Comments
wich code are you running ? |
The very last example on the linked page import fs from 'fs';
import path from 'path';
import { pipeline } from 'node:stream/promises';
const fileUrl = 'https://www.gutenberg.org/files/2701/2701-0.txt';
const outputFilePath = path.join(process.cwd(), 'moby.md');
async function downloadFile(url, outoutPath) {
const response = await fetch(url);
if (!response.ok || !response.body) {
throw new Error(`Failed to fetch ${url}. Status: ${response.status}`);
}
const fileStream = fs.createWriteStream(outoutPath);
console.log(`Downloading file from ${url} to ${outoutPath}`);
await pipeline(response.body, fileStream);
console.log('File downloaded successfully');
}
async function readFile(filePath) {
const readStream = createReadStream(filePath, { encoding: 'utf8' });
try {
for await (const chunk of readStream) {
console.log('--- File chunk start ---');
console.log(chunk);
console.log('--- File chunk end ---');
}
console.log('Finished reading the file.');
} catch (error) {
console.error(`Error reading file: ${error.message}`);
}
}
try {
await downloadFile(fileUrl, outputFilePath);
await readFile(outputFilePath);
} catch (error) {
console.error(`Error: ${error.message}`);
} |
got it yeah there are an issue with feel free to open a pr |
When you fetch the given url in the above code it fails with the error in my original post. This url
But I can fetch for other random urls that point to a text file. Since this is a learning area lesson the code should run and not fail so anyone else who are learning like me doesn't get confused. |
and if you go to the link with you browser or using |
Going to the link via browser and curl works it's only the fetch where it fails |
which version of node did you use ? |
No proxy and you can find my node |
import fs from 'node:fs';
import path from 'node:path';
import { pipeline } from 'node:stream/promises';
const fileUrl = 'https://www.gutenberg.org/files/2701/2701-0.txt';
const outputFilePath = path.join(process.cwd(), 'moby.md');
async function downloadFile(url, outoutPath) {
const response = await fetch(url);
if (!response.ok || !response.body) {
throw new Error(`Failed to fetch ${url}. Status: ${response.status}`);
}
const fileStream = fs.createWriteStream(outoutPath);
console.log(`Downloading file from ${url} to ${outoutPath}`);
await pipeline(response.body, fileStream);
console.log('File downloaded successfully');
}
async function readFile(filePath) {
const readStream = fs.createReadStream(filePath, { encoding: 'utf8' });
try {
for await (const chunk of readStream) {
console.log('--- File chunk start ---');
console.log(chunk);
console.log('--- File chunk end ---');
}
console.log('Finished reading the file.');
} catch (error) {
console.error(`Error reading file: ${error.message}`);
}
}
try {
await downloadFile(fileUrl, outputFilePath);
await readFile(outputFilePath);
} catch (error) {
console.error(`Error: ${error.message}`);
} work fine with |
Yeah. I was dreading that would be the case. That error it gave also isn't helpful enough. |
It seems this was the cause of my issues. I set the We can fix the |
Feel free to open a pr
We are here for that 😁🤗 |
Learn Lesson URL
https://nodejs.org/en/learn/manipulating-files/reading-files-with-nodejs
Problem
In the last example the code is fetching a
txt
file from this url https://www.gutenberg.org/files/2701/2701-0.txt. However when I try to run the example code the fetch fails.I tried some other books from gutenberg site and they also fail but this https://getsamplefiles.com/sample-document-files/txt works.
my node version is
v22.14.0
and I'm runningubuntu v24.04
on wsl2.Also on line 23 of the same example there is a typo
Should be
I can't see a way to add a label so I apologize for that.
The text was updated successfully, but these errors were encountered: