~/anshuman
← Back

freecodecamp backend

What is Node and what are the differences between Browser and Node Runtime Environment?

Web browsers provide the environment that is needed to run JavaScript code, including the JavaScript Engine. They also provide access to the Document Object Model (DOM), so you can access HTML elements in your code.

Previously, we could not run JavaScript outside of a browser, but now we can with Node.js.

Node.js is a JavaScript runtime environment. It's officially defined as “an open-source and cross-platform JavaScript runtime environment.”

The browser environment is primarily designed for front-end web development, so it runs client-side JavaScript. In contrast, the Node runtime environment is primarily designed for back-end web development, so it runs server-side JavaScript. This also determines the APIs that they offer.

For example, from the browser, you can access the DOM API, but there are restrictions for accessing the local file system. In contrast, from the Node runtime environment, you can access almost all system resources, including the file system, but not the DOM.

Both JavaScript environments provide a global object. In the browser JavaScript runtime, this is called window. This window object provides access to browser-related functionalities, such as access to methods for manipulating the DOM, managing cookies, and handling browser events. In the Node.js environment, this global object is called global. The global object provides access to Node.js specific functionalities, such as built-in modules for working with local files, networking, and interacting with the operating system.

What Are the Advantages and Disadvantages of Using Node on the Back-End?

Advantages:

  • One of the great advantages of Node.js is that you can implement both the front-end and the back-end of a web application using JavaScript. This reduces the initial learning curve because you and your team don't need to learn new programming languages or switch between them as you work, so you can be more efficient and productive.

  • t has a non-blocking, event-driven architecture that is great for developing real-time applications, where responsiveness and efficiency are essential for creating a good user experience.

  • Node's architecture relies on a single thread and event loop that can effectively handle a large number of simultaneous requests and input/output operations. This makes it perfect for applications that require handling multiple requests simultaneously.

  • When you work with Node.js, you also have access to npm, a package manager for Node.js. npm is a powerful tool that allows you to install and manage packages and modules for your projects.

  • You'll find packages for many different purposes, including handling HTTP requests, working with databases, formatting dates and times, testing code, and more. By using packages, you can reuse code that was already written, tested, and shared by other developers to make your workflow faster and more efficient.

Disadvantages:

  • As you learned, it runs your JavaScript on a single thread, which means it can only execute one piece of your code at a time. While its architecture allows it to be efficient for handling concurrent (simultaneous) requests, CPU-intensive tasks may block the main thread and result in performance issues.

Examples of CPU-intensive tasks include complex mathematical operations, image and video processing, and cryptography.

  • The asynchronous nature of Node.js can potentially make the code more difficult to read, understand, and debug.
  • And finally, you should also be careful when choosing packages from npm, the Node package manager, because some of them may not be constantly maintained, so they may introduce vulnerabilities into your own application.

How Can you install Node on your computer?

Before you install Node.js, we recommend installing a tool called NVM (Node Version Manager). As its name implies, NVM is a tool for managing multiple versions of Node.js on the same computer.

Setting up this tool from the start will help you in the future because you might need to switch between different versions of Node.js for different projects. With NVM, you can do this very easily.

Let's see how you can install NVM on macOS/Linux and Windows, and how you can use it to install Node.js.

How to Install NVM on macOS/Linux

On macOS and Linux, you can install NVM directly through a shell script. But for macOS, note that the official documentation for NVM mentions that "you need to manually install the Xcode command line tools before running the install script, otherwise, it'll fail."

On macOS, you can install Xcode command line tools by running this command in your terminal:

xcode-select --install

Once the installation is finished, you can check the version number and verify the installation with this command:

xcode-select --version

Then, it's time to install NVM. You can download and execute the NVM installation script by running one of these commands:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

To get the latest version of NVM at the time when you are reading this lesson, please refer to the official NVM documentation: https://github.com/nvm-sh/nvm?tab=readme-ov-file#install--update-script. There, you will find these commands with the latest version number.

You might need to restart your terminal when the process is completed to load the new command and the new configuration.

Then, run this command to confirm that NVM was installed correctly and to check your current version:

nvm -v

You should see your version of NVM in the output.

How to Install Node.js with NVM on macOS/Linux

Now that you have NVM installed on macOS/Linux, you can install Node.js.

To install the latest Long-Term Support (LTS) version of Node.js, run this command:

nvm install --lts

Notice the --lts flag. This flag indicates that you want to install the current Long-Term Support (LTS) version of Node.js.

Alternatively, you can install a specific version of Node.js by specifying the version number in the command:

nvm install <version>

For example, here's how you can install Node.js v20:

nvm install 20

Once you finish installing a version of Node.js, you can switch to that version with this command:

nvm use <version>

For example, here's how to switch to version 20:

nvm use 20

Finally, you can check the current version of Node.js with this command:

node -v

How to Install NVM on Windows

However, for Windows itself, there are a few alternatives. One of them is nvm-windows, a Node.js version management utility for Windows. This is a completely separate project from the official NVM project. But the purpose of these tools is the same: to help you work with multiple versions of Node.js on the same device.

Before installing nvm-windows, you should know that the official repository recommends uninstalling any prior Node installation before installing NVM for Windows. If this is your first time installing Node.js, you should be okay. If you already have other versions of Node.js installed, it is recommended to uninstall them first.

To install nvm-windows:

  • Go to the Releases section of the official repository: https://github.com/coreybutler/nvm-windows/releases.
  • Download the latest installer.
  • Follow the instructions. After completing the installation, you might need to restart your terminal if the nvm command is not immediately available.

You can check if the installation was successful and see your current version of nvm-windows by running this command:

nvm -v

How to Install Node.js Using NVM for Windows

Now that you have nvm-windows installed on Windows, you are ready to install Node.js.

To install the latest Long-Term Support (LTS) version of Node.js, run this command:

nvm install lts

The Long-Term Support (LTS) version is usually recommended, but you can install a different version by specifying its version number in the command:

nvm install <version>

For example:

nvm install 20

When the installation is completed, you can switch to that version of Node.js with this command:

nvm use <version>

For example:

nvm use 20

You can also check your current version of Node.js with this command:

node -v

Basic Commands to work with Node.js and NVM

Also, while all these commands will work for NVM installed on macOS/Linux, including WSL, they may work differently or may not work at all for nvm-windows. We'll leave alternative commands for nvm-windows whenever possible.

How to Adjust PowerShell's Execution Policy

If you're running Windows and are following along with PowerShell and nvm-windows, you may need to adjust PowerShell's execution policy. The default execution policy for PowerShell is Restricted, which is a security measure to prevent scripts from running.

  • First, run PowerShell as an administrator by right-clicking on PowerShell, then clicking "Run as Administrator".

  • Then, to see the current execution policy of the current PowerShell session, run the following command:

Get-ExecutionPolicy

Finally, to set PowerShell's execution policy to RemoteSigned, which allows local scripts and npm to run, use the following command:

Set-ExecutionPolicy RemoteSigned

Once you've run the command above, you can close the administrator window of PowerShell, and open a non-admin PowerShell window.

List All Versions

To list all installed versions of Node.js and check the one that is currently being used, run this command:

nvm ls

Set Default Node.js Version (NVM on macOS/Linux Only)

This command sets the default version of Node.js that will be used for new terminal sessions:

nvm alias default <version>

For example:

nvm alias default 22.20.0

You can also create custom aliases to assign a more specific name to a version:

nvm alias <new_name> <version>

For example:

nvm alias new-api 18.20.8

After setting up the alias, you can use this new name to switch to that version:

nvm use <new_name>

For example:

nvm use new-api

Remove a Version of Node.js

To remove a version of Node.js, run this command:

nvm uninstall <version>

For example:

nvm uninstall 20.19.5

With NVM for macOS/Linux, you can remove a version of Node.js by running the uninstall command with a major version like 22 or 20. For example:

nvm uninstall 20

If you have multiple releases for that major version installed like 20.14.0 and 20.12.2, the latest version will be removed first. In this case, running nvm uninstall 20 will remove 20.14.0 first.

Check Your Version of Nodejs

node -v

Check Your Version of NPM

npm -v

Run a Nodejs File

node server.js

Create a Nodejs Project

npm init

When you run the npm init command, it will walk you through an interactive CLI guide for setting up the package.json file. It will ask you questions for the project name, description, version, etc. If you want to start with a default package.json file, you can use the npm init -y or npm init --yes command.