Downloading a Webpage Using Node.js
This simple Node.js script can be a helpful tool for accessing the contents and data from any webpage.
Prerequisites
- Node.js installed on your computer. You can verify node is installed by running
node -v
in your terminal. At the time of writing, the active LTS release is v16.15.1 - NPM installed on your computer. This should come as part of the Node.js installation. You can verify npm is installed by running
npm -v
in your terminal. At the time of writing, the latest version is 8.11.0.
Step 1: Set up folder for the Node script
Using your terminal or file explorer create a new folder for the project. For this tutorial I’ll call the folder html-fetch.
Using terminal, this can be accomplished by using the cd
command to navigate to the folder where we want to create the project, then running mkdir html-fetch
to create the folder.
Step 2: Initialize the project
In terminal, navigate into the newly created folder by running cd html-fetch
.
Run npm init --yes
to initialize the package. The --yes
option will skip the questionnaire and give us the default configuration.
This should output the contents of the package.json file in your terminal and create a package.json file in the folder.
{
"name": "html-fetch",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test"…