Chime Princewill I am a passionate MERN stack engineer currently working with Descasio as a full-stack engineer.

A guide to using Node on Windows with Chocolatey

4 min read 1144 112

A Guide To Using Node With Chocolatey On Windows

If you’ve used a Linux or Mac system as a developer, you may well agree with me that their flexible and secure options for managing packages are second to none.

Package managers like apt-get for Linux and Homebrew for Mac have greatly improved the installation speed, configuration, and security of packages for our projects.

Well, if you’re a Windows user, then do not fear! Guess what? Thanks to Chocolatey, you can automatically install packages and software and deploy packages right from your Windows terminal — all without having to manually jump through hoops to install and set them up.

What we will cover in this Chocolatey guide

In this article, I will guide you on how to set up Chocolatey to do the following right from your Windows system.

Introduction to Chocolatey

Chocolatey is a Windows package manager that helps install, upgrade, and uninstall packages, and more.

You can think of it as similar to the aforementioned APT or Homebrew. Its ability to scan through installed packages for virus detection means you can be confident that packages remain secure.

Requirements

  • Windows
  • PowerShell
  • Working knowledge of JavaScript

Windows package managers

Unlike other package managers like Winget, RuckZuck, and Yarn, Chocolatey brings several benefits with it, such as:

  • Large number of applications/utilities available
  • Installs without bloatware
  • You can put Chocolatey install commands into your PowerShell scripts
  • Easy to use
  • Downloaded files are verified by checksums
  • Free and open source
  • Builds on technology already familiar to devs
  • Decentralized package source

Installation

Let’s navigate to the command line (CMD). Open it as an admin and run the following PowerShell scripts:

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

Resolve installation issues

While installing Chocolatey, you may run into the following error shown in the code snippet image here:

Error Code Snippet Installation Chocolatey

Open PowerShell as an admin and set the required rights, as shown below:

 Get-ExecutionPolicy

If this response returns:

Restricted

Then run the following code snippet:

 Get-ExecutionPolicy AllSigned

After running the code snippet above, you will be prompted to respond “Yes” to secure your system against scripts you don’t trust. Once you’ve done so, rerun the code snippet and you should see this:

Code Rerun Get-ExecutionPolicy

Currently, an added security check has been set for scripts. Let’s go back to the command-line interface and continue from where we left off.

If you look closely at the error message that was encountered earlier, a suggestion to set the environment variable for Chocolatey was made. So, let’s set the environment variable, as shown below:

Environment Variable

Now we’ve set the variable with the above code snippet, let’s try to install Chocolatey again, as demonstrated here:

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

If the code snippet above was successful, the command-line interface should look like this:

Command-line Interface Successful Installation

To see the version of Chocolatey that was installed, you can run the following code snippet:

Check Chocolatey Version

To execute Chocolatey, all you need to do is to open the command line and run:

choco

Installing packages

To start installing packages, close the CMD and reopen it as an admin once again.

Now, let’s see how Chocolatey can be used to install NVM (Node Version Manager), as shown here:

choco install nvm.portable

Let’s explain this further: to install a package, the keyword choco is used, then install, and then the name of the package itself.

NVM (Node Version Manager) is a package used to manage the installation of different versions of Node.js and npm. There are also many more packages that are available to install.

Every installed package is saved in one location and can be found in the following path:

 'C:\ProgramData\chocolatey\lib'

To locate a single package, you must add the package name to the lib path, like this:

'C:\ProgramData\chocolatey\lib\yourPackageName'

Using NVM to install Node.js and npm

Let’s go into the terminal and run the following code snippet:

nvm install lts

The snippet above will install the latest stable version of Node.js and npm. A specific version of Node can also be installed by running the following:

nvm install 16.18.0

To see the list of Node versions that can be selected, run the following:

List Of Node Versions That Can Be Selected

This is a list of Node versions that have been installed, including the one I am currently using on my Windows machine. Now, let’s see how a specific Node version is selected — take a look here:

nvm use 16.18.0

Run the code snippet to check the version of Node and npm we are currently using:

Current Version Of Node

Running REPL on the Node environment

JavaScript Code Right From The Node Environment

In the code snippet above, I was able to write my normal JavaScript code right from the Node environment, meaning I can calculate numbers, create loops and functions, and read variables directly from the terminal.

N.B., all that is necessary is to write node and hit enter to start up the Node environment on the terminal

Uninstall Node.js

To uninstall Node, you just need to specify the Node version to uninstall, as shown here:

nvm uninstall node 16.18.0

Upgrade packages

To upgrade a package that has been installed using Chocolatey, let’s run the following code snippet:

choco upgrade nvm.portable

The code snippet above will search for the NVM package inside the lib location. Once it finds it, it will look for a possible stable release and upgrade to the relevant version.

Retrieve information about a specific package

To get all available information about a package, we can run the following code snippet:

choco info nvm.portable

In the response displayed below, you will find pertinent information regarding the package you queried.



Information Regarding Specific Package

Install multiple packages

To install multiple packages, simply run the following code snippet:

choco install nvm.portable firefox

In the snippet above, you can see that, in the same way multiple files work with npm, it’s also true of Chocolatey — we simply stack each package after the other.

Uninstall packages

To uninstall a package installed using choco, run the following code snippet:

choco uninstall nvm.portable

Conclusion

Chocolatey is a great choice as your package manager for your Windows packages. The flexibility of using packages in your projects just the same way you always have means that devs are not required to learn anything new and the barrier to entry is low as a result, which is great!

200’s only Monitor failed and slow network requests in production

Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If you’re interested in ensuring requests to the backend or third party services are successful, try LogRocket. https://logrocket.com/signup/

LogRocket is like a DVR for web and mobile apps, recording literally everything that happens while a user interacts with your app. Instead of guessing why problems happen, you can aggregate and report on problematic network requests to quickly understand the root cause.

LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, slow network requests, and also logs Redux, NgRx, and Vuex actions/state. .
Chime Princewill I am a passionate MERN stack engineer currently working with Descasio as a full-stack engineer.

Leave a Reply