How do I download a file with Node.js without using third-party libraries?
I don’t need anything special. I only want to download a file from a given URL, and then save it to a given directory.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As of Node 18, you can use the built-in
fetchglobal, which implements the Fetch API to download data with several methods built in to directly work with the result as plain text, JS-converted-from-JSON, or binary data (as ArrayBuffer).For older versions of Node, you can create an HTTP
GETrequest and pipe itsresponseinto a writable file stream:If you want to support gathering information on the command line–like specifying a target file or directory, or URL–check out something like Commander.
More detailed explanation in https://sebhastian.com/nodejs-download-file/