What is Chocolatey and how to install Node.js using Chocolatey?

Published on 20 March 2023 08:57 PM
This post thumbnail

Chocolatey and Node.js are two popular tools in the software development world. While Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, Chocolatey is a package manager for Windows operating system. Chocolatey can be compared with other package managers such as apt-get (for Linux), Homebrew (for macOS), and Scoop (for Windows).

In this blog, we will discuss the benefits of using Chocolatey to manage the installation and upgrades of Node.js and compare it with other similar package managers.

What is Chocolatey?

Chocolatey is a free, open-source package manager for Windows that makes it easy to install, upgrade, and manage software packages on Windows computers. It is a command-line tool that automates the process of downloading, installing, and upgrading software packages, so you don't have to do it manually. With Chocolatey, you can easily install software packages with a simple command line, such as choco install nodejs.

The main goal of Chocolatey is to make software management on Windows more convenient, efficient, and reliable. Chocolatey works by using packages, which are pre-configured collections of software and related information. Packages can be installed using the Chocolatey command line interface (CLI) and provide an easy way to manage software on Windows.

Chocolatey has a large repository of packages, which can be searched and installed using the CLI. Packages in the repository are maintained by the Chocolatey community, which includes both individual users and organizations. Packages can be created by anyone and can be submitted to the repository for other users to use.

What is Node.js?

Node.js is a server-side, cross-platform, open-source JavaScript runtime environment. It was created in 2009 by Ryan Dahl and has since become one of the most popular platforms for building server-side applications.

Node.js is built on the V8 JavaScript engine, which was developed by Google for use in the Chrome web browser. The V8 engine provides fast, high-performance JavaScript execution, making it an ideal platform for building server-side applications.

Node.js uses an event-driven, non-blocking I/O model, which makes it well-suited for building scalable, high-performance network applications. In this model, a single thread can handle multiple requests simultaneously, allowing Node.js applications to handle a large number of simultaneous connections with a small amount of memory.

According to the Node.js website, there are over 1 million weekly downloads of Node.js and over 500,000 active users of npm, which is the default package manager for Node.js. Additionally, Node.js has a large and active community of developers who contribute to the project, write packages, and provide support to other users.

Why Chocolatey is popular than other Scoop window package managers?

Chocolatey is a popular package manager for Windows and has been gaining popularity in recent years. According to its website, Chocolatey has over 5 million downloads per month and is used by many organizations and individuals for managing software installations on Windows machines. It contains over 178k packages for a wide variety of software applications and tools. This makes it easy for users to find and install the software they need, without having to manually download and install each package.

Chocolatey vs Scoop

Chocolatey and Scoop are both package managers for Windows operating systems. Here is a comparison of the two:

  1. Repository size: Chocolatey contains over 178k plus packages compared to Scoop which has 1k plus packages. This means that it is more likely that you will find the software you need in the Chocolatey repository.

  2. Installation process: The installation process for Chocolatey is slightly more complex compared to Scoop. With Scoop, you can install it using a single command in PowerShell, while with Chocolatey, you need to run a PowerShell script.

  3. Package management: Both Chocolatey and Scoop provide a convenient way to install and manage software packages on Windows. However, Scoop has a more limited set of features compared to Chocolatey, such as the ability to install applications directly from GitHub.

  4. User community: Chocolatey has a larger and more established user community compared to Scoop. This means that you are more likely to find help and support for Chocolatey if you need it.

Both Chocolatey and Scoop are useful tools for managing software packages on Windows. The best package manager for you will depend on your specific needs and preferences. For example, if you want a simple and easy-to-use package manager, Scoop might be the best choice for you. On the other hand, if you need a package manager with a large repository of packages and a more established user community, Chocolatey might be the better option.

Why use Chocolatey to install Node.js?

There are several benefits of using Chocolatey to install and manage Node.js on your Windows machine:

  1. Easy to install and upgrade: With Chocolatey, you can install Node.js with a simple command line, such as choco install nodejs. This makes it much easier to install and upgrade Node.js on your computer, especially if you need to do it on multiple machines. choco upgrade nodejs

  2. Consistent versioning: Chocolatey ensures that you have the same version of Node.js installed on all your machines, so you don't have to worry about compatibility issues between different versions.

  3. Automated process: Chocolatey automates the process of downloading, installing, and upgrading Node.js, so you don't have to do it manually. This saves you time and ensures that you have the latest version of Node.js installed.

  4. Centralized management: With Chocolatey, you can manage the installation and upgrades of Node.js from a central location, making it easier to manage your software packages on multiple machines.

How to Install Chocolatey?

To install Chocolatey on Windows, follow these steps:

  1. Click Start and type Powershell

  2. Right-click Windows Powershell and choose Run as Administrator

  3. Paste the following command into Powershell and press enter.

    Set-ExecutionPolicy Bypass -Scope Process -Force; `
    iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
    
  4. Answer Yes when prompted

  5. Close and reopen an elevated PowerShell window to start using choco

  6. You can check if choco is installed by running command choco --version

    node undefined error

Note: The installation process requires administrative privileges, so make sure you run the Command Prompt window as an administrator.

How to install node.js?

Once Chocolatey is installed, you can use it to install Node.js. Here's how:

  1. Open a Command Prompt window.

  2. If you wanna install the latest version of nodejs then run command below

    choco install nodejs
    

    or if you want to install specific version then run command below

    choco install nodejs --version=[VERSION_NUMBER]  
    
  3. Wait for the installation to complete, and then close the Command Prompt window. I have installed the nodejs version for this tutorial.

  4. To check if nodejs is installed or not, you can run the command below:

    nodejs --version
    
  5. If installation is successful, then you will be able to see:

    node version

  6. Just in case, if you get the error like in the screenshot below, then follow the further instructions below. node undefined error

In case you got you error as per point number 6, do following:

  1. Open the Command Prompt or PowerShell as an administrator.

  2. Locate the path to the Node.js executable by searching for it on your system. The executable is usually located in the C:\Program Files\nodejs directory.

  3. Copy the path to the Node.js executable.

  4. Open the System Properties window by right-clicking on the "This PC" or "My Computer" icon and selecting "Properties" from the context menu.

  5. Click on the "Advanced system settings" link.

  6. Click on the "Environment Variables" button.

  7. Under the "System variables" section, scroll down and select the "Path" variable, then click on the "Edit" button.

  8. Click on the "New" button and paste the path to the Node.js executable file that you copied earlier.

  9. Click "OK" to close all the windows and save the changes.

  10. Close and reopen the Command Prompt or PowerShell.

  11. Run the node -v command again to check if the issue has been resolved

How to use specific version of Node.js

Chocolatey itself doesn't manage which version of Node.js is used, but you can use a tool like NVM (Node Version Manager) to switch between different versions of Node.js on Windows.

To install NVM using Chocolatey, you can run the following command in an elevated Command Prompt or PowerShell:

choco install nvm

Once NVM is installed, you can use the following command to install a specific version of Node.js:

nvm install [VERSION_NUMBER]

Replace [VERSION_NUMBER] with the specific version number you want to install. For example, to install Node.js version 14.17.6, the command would be:

nvm install 14.17.6

Now to switch to specific Node.js version, run the following command:

nvm use 14.17.6

This will set the specified version of Node.js as the default version to be used in your current command prompt session.

Note: NVM works on a per-shell basis, so you'll need to run the nvm use command every time you open a new command prompt window if you want to use a specific version of Node.js by default.

How to uninstall Node.js or any other packages in choco

Uninstalling any packages is as easy as installing a package in choco. You just need to run

choco uninstall [PACKAGE_NAME]

For example, if you want to uninstall node.js then run:

choco uninstall nodejs

In case if you have multiple version of Node.js then you will need to specify the version while you uninstall like:

choco uninstall nodejs --version 14.17.6

Note: This command will only remove the Chocolatey package for Node.js and npm, but it will not remove any additional files or dependencies that may have been installed alongside Node.js. To completely remove Node.js and all associated files, you may need to manually delete any remaining files and folders.

Conclusion

In conclusion, Chocolatey is a useful tool for managing the installation and upgrades of Node.js on Windows machines. With its easy-to-use command line interface, consistent versioning, automated process, and centralized management, it makes it much easier to install and manage Node.js on your computer. Whether you are a developer or a system administrator, Chocolatey is a great tool to have in your toolkit.