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

javascript - Downloading file in typescript in Node.js - Stack Overflow

programmeradmin0浏览0评论

I am trying to download a file with typescript (Node.js) using node-fetch.

As per the documentation and a stack overflow answer here the following code should work:

public async downloadXMLFeed(): Promise<void>{
    // function for download the file to
    // a temporary location
    let fileStream = fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
    fetch(FILE_URL)
    .then((response) => {

        response.body.pipe(fileStream)

        fileStream.on("finish", () => {
            fileStream.close();
        })
    });

but I get the following error:

Property 'pipe' does not exist on type 'Response'

I also checked the type file for response in node-fetch types. It does not have a pipe function.

I checked the type definition for node-fetch here and it seems the body in response is a readable stream as per the interface here:

export class Body {
   constructor(body?: any, opts?: { size?: number; timeout?: number });
   arrayBuffer(): Promise<ArrayBuffer>;
   blob(): Promise<Buffer>;
   body: NodeJS.ReadableStream; // This should work.
   bodyUsed: boolean;
   buffer(): Promise<Buffer>;
   json(): Promise<any>;
   size: number;
   text(): Promise<string>;
   textConverted(): Promise<string>;
   timeout: number;

}

and has a function pipeTo (I found this from the documentation here. I tried run the following code after going through the mentioned documentation:

public async downloadXMLFeed(): Promise<void>{
    // function for download the file to
    // a temporary location
    let fileStream = fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
    fetch(FILE_URL)
    .then((response) => {

        response.body.pipe(fileStream)

        fileStream.on("finish", () => {
            fileStream.close();
        })
    });

However I get the error:

Argument of type 'WriteStream' is not assignable to parameter of type 'WritableStream<Uint8Array>'.

Type 'WriteStream' is missing the following properties from type 'WritableStream': locked, abort, getWriter

Now the following code:

 fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});

returns an object with type WriteStream (checked here) which implements stream.Writable. So I don't understands why doesn't the WriteStream object have the mentioned functions.

Also, how do I solve this? Is there a standard way of downloading http files in typescript with a node.js backend that I am not being able to figure out? Or am I missing something here.

I am trying to download a file with typescript (Node.js) using node-fetch.

As per the documentation and a stack overflow answer here the following code should work:

public async downloadXMLFeed(): Promise<void>{
    // function for download the file to
    // a temporary location
    let fileStream = fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
    fetch(FILE_URL)
    .then((response) => {

        response.body.pipe(fileStream)

        fileStream.on("finish", () => {
            fileStream.close();
        })
    });

but I get the following error:

Property 'pipe' does not exist on type 'Response'

I also checked the type file for response in node-fetch types. It does not have a pipe function.

I checked the type definition for node-fetch here and it seems the body in response is a readable stream as per the interface here:

export class Body {
   constructor(body?: any, opts?: { size?: number; timeout?: number });
   arrayBuffer(): Promise<ArrayBuffer>;
   blob(): Promise<Buffer>;
   body: NodeJS.ReadableStream; // This should work.
   bodyUsed: boolean;
   buffer(): Promise<Buffer>;
   json(): Promise<any>;
   size: number;
   text(): Promise<string>;
   textConverted(): Promise<string>;
   timeout: number;

}

and has a function pipeTo (I found this from the documentation here. I tried run the following code after going through the mentioned documentation:

public async downloadXMLFeed(): Promise<void>{
    // function for download the file to
    // a temporary location
    let fileStream = fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
    fetch(FILE_URL)
    .then((response) => {

        response.body.pipe(fileStream)

        fileStream.on("finish", () => {
            fileStream.close();
        })
    });

However I get the error:

Argument of type 'WriteStream' is not assignable to parameter of type 'WritableStream<Uint8Array>'.

Type 'WriteStream' is missing the following properties from type 'WritableStream': locked, abort, getWriter

Now the following code:

 fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});

returns an object with type WriteStream (checked here) which implements stream.Writable. So I don't understands why doesn't the WriteStream object have the mentioned functions.

Also, how do I solve this? Is there a standard way of downloading http files in typescript with a node.js backend that I am not being able to figure out? Or am I missing something here.

Share Improve this question asked Jan 11, 2020 at 6:52 AvneeshAvneesh 3553 silver badges13 bronze badges 2
  • Have you installed the package @types/node-fetch? If so then you should't have problems with this or that. – winwiz1 Commented Jan 11, 2020 at 7:20
  • Yes. I have installed the type definition for node-fetch. – Avneesh Commented Jan 11, 2020 at 7:32
Add a ment  | 

1 Answer 1

Reset to default 2

but I get the following error:

Property 'pipe' does not exist on type 'Response'

This error is also triggered by this code

async function download() {
  const res = await fetch('https://assets-cdn.github./images/modules/logos_page/Octocat.png');
  await new Promise((resolve, reject) => {
    const fileStream = fs.createWriteStream('./octocat.png');
    res.body.pipe(fileStream);
    res!.body!.on("error", (err) => {
      reject(err);
    });
    fileStream.on("finish", function() {
      resolve();
    });
  });
}

taken from here. The error is triggered because the fetch function is interpreted as JS fetch.

To fix the error add import nodeFetch from "node-fetch"; and replace fetch(...) with nodeFetch(...).

发布评论

评论列表(0)

  1. 暂无评论