π― Introduction
JavaScript is a powerful language, often run in the browser to manipulate the DOM and add interactivity. However, when working with advanced JavaScript frameworks like React or tools like Jest for automated testing, it’s necessary to run JavaScript outside of the browser. This is where Node.js comes in.
Node.js is an open-source JavaScript runtime that allows you to run JavaScript code in your development environment, independent of a browser. To take advantage of Node.js in your local environment and manage different versions of it (as required by various frameworks), you need to install Node.js and use a tool to manage multiple versions.
In this post, weβll walk through the process of installing and maintaining Node.js on your Windows machine, using Volta, a tool for managing JavaScript versions.
π§ Part 1: Install Volta
Before we install Node.js, we need to install Volta, a tool manager for JavaScript. Volta allows us to manage different versions of Node.js, making it easy to switch between versions when necessary.
Step 1: Open the Command Prompt
- Click on the Start Menu (or press the Windows key) and type
cmd
. - Select Command Prompt from the search results to open it.
Step 2: Check if Volta is Already Installed
In the Command Prompt, enter the following command:
- If Volta is already installed, youβll see an output listing the available tools.
- If you see the error
βvoltaβ is not recognised...
, proceed to the next step to install Volta.
Step 3: Install Volta
- In the Command Prompt, enter the following command to install Volta:
You may need to agree to the source agreement terms by typing
Y
and pressing Enter. - Once the installation is complete, you can close the Command Prompt.
π¦ Part 2: Install Node.js
With Volta installed, letβs now install Node.js. We will install Node.js v22, which is the LTS (Long Term Support) version. LTS versions are stable and reliable, making them ideal for development purposes.
Step 1: Open Visual Studio Code (VS Code)
You donβt need to open any specific directory. Just open VS Code and launch a new terminal by selecting Terminal > New Terminal from the top toolbar.
Step 2: Verify if Node.js v22 is Already Installed
In the VS Code terminal, enter:
- If Node.js v22 is already installed, youβll see something like
v22.x.x
. - If Node.js isnβt installed, proceed with the next steps.
Step 3: Install Node.js v22
In the VS Code terminal, enter the following command to install Node.js v22:
Step 4: Verify Installation
After the installation is complete, enter the following command to confirm that Node.js v22 is active:
You should see an output showing Node.js v22.x.x. To ensure Node.js is correctly installed, check the version by running:
This should return v22.x.x
, confirming the installation was successful.
π Part 3: Install Another Version of Node.js
If you need to work with a different version of Node.js for another project, Volta makes it simple to install and switch between versions.
Step 1: Install Node.js v16
Letβs install Node.js v16 as another version to work with:
Step 2: Verify Installation
After installation, run:
This will list all installed Node.js versions. Run:
It should return v16.x.x
, confirming that Node.js v16 is now active.
π Part 4: Switching Between Installed Versions of Node.js
You can easily switch between installed versions of Node.js using Volta.
Step 1: Switch to Node.js v22
To switch back to Node.js v22, run the following command:
Step 2: Verify Active Version
Run the following command again to verify that v22 is active:
And check the active version with:
You should see v22.x.x
as the active version.
Summary of Version Switching
- Installing a version:
volta install node@<version>
- Switching to a version:
volta install node@<version>
- Verifying active version:
volta ls
ornode -v
π Conclusion: Installing Node.js on Windows with Volta
With Volta installed on your Windows machine, managing Node.js versions becomes quick and easy. You can easily install different versions of Node.js, switch between them, and ensure that youβre using the right version for each project.
By following this guide, you can confidently set up Node.js in your development environment and maintain different versions as needed. Happy coding!