Skip to content
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

Closed
GaneshS288 opened this issue Feb 24, 2025 · 12 comments
Closed

Reading files with nodeJS typo and fetch failure with example link #7510

GaneshS288 opened this issue Feb 24, 2025 · 12 comments

Comments

@GaneshS288
Copy link
Contributor

GaneshS288 commented Feb 24, 2025

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.

ganesh@LAPTOP-G6NM8GVN:~/repos/node-practice$ node app.js
Debugger attached.
TypeError: fetch failed
    at node:internal/deps/undici/undici:13502:13
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async downloadFile (file:///home/ganesh/repos/node-practice/app.js:10:22)
    at async file:///home/ganesh/repos/node-practice/app.js:44:3 {
  [cause]: AggregateError [ETIMEDOUT]: 
      at internalConnectMultiple (node:net:1139:18)
      at internalConnectMultiple (node:net:1215:5)
      at Timeout.internalConnectMultipleTimeout (node:net:1739:5)
      at listOnTimeout (node:internal/timers:596:11)
      at process.processTimers (node:internal/timers:529:7) {
    code: 'ETIMEDOUT',
    [errors]: [ [Error], [Error] ]
  }
}
Error: fetch failed

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 running ubuntu v24.04 on wsl2.

Also on line 23 of the same example there is a typo

const readStream = createReadStream(filePath, { encoding: 'utf8' });

Should be

const readStream = fs.createReadStream(filePath, { encoding: 'utf8' });

I can't see a way to add a label so I apologize for that.

@AugustinMauroy
Copy link
Member

wich code are you running ?

@GaneshS288
Copy link
Contributor Author

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}`);
}

@AugustinMauroy
Copy link
Member

AugustinMauroy commented Feb 24, 2025

got it yeah there are an issue with fs but I didn't get what is your fetch issue ?

feel free to open a pr

@GaneshS288
Copy link
Contributor Author

GaneshS288 commented Feb 24, 2025

When you fetch the given url in the above code it fails with the error in my original post.

This url

https://www.gutenberg.org/files/2701/2701-0.txt

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.

@AugustinMauroy
Copy link
Member

and if you go to the link with you browser or using curl what happened
because the link work on my side

@GaneshS288
Copy link
Contributor Author

Going to the link via browser and curl works it's only the fetch where it fails

@AugustinMauroy
Copy link
Member

which version of node did you use ?
and are you behind a http proxy ?

@GaneshS288
Copy link
Contributor Author

No proxy and you can find my node v22.14.0 as well as os version in original post

@AugustinMauroy
Copy link
Member

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 22.14.0 on my machine

@GaneshS288
Copy link
Contributor Author

GaneshS288 commented Feb 24, 2025

Yeah. I was dreading that would be the case.
I tried fetching other types of resources like jpegs/png and txt files from other sites and they work.
It's only project gutenberg that doesn't.

That error it gave also isn't helpful enough.

@GaneshS288
Copy link
Contributor Author

nodejs/undici#2777 (comment)

It seems this was the cause of my issues. I set the network-family-autoselection-attempt-timeout=500 and it worked then. I don't have enough knowledge about node to know how this works but this proves my fetch issue is unrelated here.

We can fix the fs.createReadStream typo and close this issue. Sorry for taking your time

@AugustinMauroy
Copy link
Member

We can fix the fs.createReadStream typo and close this issue.

Feel free to open a pr

Sorry for taking your time

We are here for that 😁🤗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

No branches or pull requests

2 participants