I am having problems downloading a zip file from the internet. I found countless of examples, but all return the same issue:
I work in NodeJS 12 LTS and Electron 10.
core.js:4197 ERROR Error [ERR_STREAM_CANNOT_PIPE]: Cannot pipe, not readable
at ClientRequest.pipe (_http_outgoing.js:887)
The examples I found are e.g. this one here:
import { request } from 'https';
import * as fs from "fs";
request("https://nodejs.org/dist/v12.18.4/node-v12.18.4-win-x86.zip")
.pipe(fs.createWriteStream('/Users/foo/Desktop/bas.zip'))
.on('close', function () {
console.log('File written!');
});
It couldn't be easier than that, but nonetheless, this fails. The order should be like this (here)
readable.pipe(writable);
This error message doesn't make any sense. What am I missing here?
Per the docs, https.request returns an instance of ClientRequest - a writable stream which you can pipe to in order to send files with your request. Trying to pipe from this writable stream into a file is the source of your error - you need a readable stream of the response instead.
To get the readable stream of the response, you should add a callback and pipe it from the response.
request("https://nodejs.org/dist/v12.18.4/node-v12.18.4-win-x86.zip", res => {
res.pipe(fs.createWriteStream('/Users/foo/Desktop/bas.zip'))
.on('close', function () {
console.log('File written!');
});
});
The native https api is not very friendly. What you originally wrote would have worked with the now-deprecated request module, and perhaps you got it from a code snippet using the very same. Nowadays, we should use an updated module like got. Here's an example:
import * as got from 'got';
import * as fs from "fs";
got.stream("https://nodejs.org/dist/v12.18.4/node-v12.18.4-win-x86.zip")
.pipe(fs.createWriteStream('/Users/foo/Desktop/bas.zip'))
.on('close', function () {
console.log('File written!');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With