Game World!

Join A World Of Gamers

Enter your email address:

Delivered by FeedBurner

Followers

Popular Posts

Wednesday 30 June 2021

How do I install NPM on Mac?

 

How to install Node.js and npm on macOS

In this post, we'll be looking at three different ways to install Node.js and its package manager, Node Package Manager, on macOS. We will discuss how to install Node.js using mac-native package installer, homebrew, a 3rd party installer, Node Version Manager and how to hop between different Node.js versions using Node Version Manager

A simple guide on how to set up Node.js development environment on macOS.

Node.js is an open-source runtime environment, which allows developers to create networked applications and web-servers in JavaScript. The main thing to remember is that Node.js is asynchronous and event-driven, which means it can support hundreds of simultaneous calls in the event loop. Basically, this means that a Node-based server does not wait for an API to return data. You can see a great example of asynchronous programming in Node.js here.

Node.js therefore shines in building fast, data-intensive, and real-time network applications, such as Uber and PayPal. However, you can also use Node.js to build your private blockchain.

When using Node.js, you’ll also need to use Node Package Manager, or npm for short. npm is a package management framework for Node.js. It provides a command line utility tool to install Node.js libraries and manage their versions and dependencies. If you’ve ever used other programming languages, such as Ruby or Python, then you’ll notice that npm is analogous to rubygems in Ruby or pip / poetry in Python.

In this post, we'll learn how to install Node.js and Node Package Manager (NPM) in a macOS environment.

What you will learn:#

By the end of this tutorial you will have a good grasp on the following:

  • How to install Node.js and NPM using the macOS installer available from the Node.js website

  • How to install Node.js and NPM using homebrew, arguably the most popular package manager for macOS

  • How to install Node.js and NPM using Node Version Manager, or nvm for short. This is by far the best method to set up Node.js on your system because it gives you control and flexibility when hopping between different projects and their requirements.

  • How to switch between different Node.js versions using nvm

This post is intended for complete beginners to JavaScript or for folks switching from Ruby or Python to JavaScript for their backend production. It doesn’t assume any background knowledge of JavaScript (although it doesn’t hurt to have some!). However, this tutorial will cover everything you need to know and hopefully will get you up to speed in no time!

Ready? Let’s jump in.

To check whether you already have Node installed, open new terminal window and type:

If you have Node.js installed, it should output Node’s version. If you don’t, you’ll see one of the two messages, depending on whether you use bash or zsh shell:

- bash: command not found: node

- zsh: command not found: node

That means that the command you are trying to run is not installed. But worry not, there are several ways to install Node.js and NPM:

  1. using the macOS installer available from the Node.js website

  2. using homebrew

  3. using Node Version Manager (recommended)


I’ll go over each method step-by-step.

1) Using the macOS installer available from the Node.js website#

Visit the Node.js website where you can download a pre-built installer for your mac platform.

There are two types of Node.js releases: long-term support (LTS) and current. LTS releases are more polished and bug-free and will suffice for the majority of everyday users. Current releases are typically more experimental and contain the latest features, which may not be completely finished and could occasionally crash. You can switch between LTS and current versions by highlighting the field in the first tab. Again, the LTS version is recommended for most users. Therefore, if the LTS tab is highlighted in dark green, you can simply click on the macOS Installer option, which will download the .pkg installer for Node.js.



This installer will also automatically install NPM for you. Just follow these steps:

  1. Download the Node.js .pkg Installer:

    • Opening node-v12.18.0.pkg

      • Select Open with Installer (default)

  2. Run the .pkg Installer and follow the instructions that will guide you through the interface:

    • Introduction

      • Select Continue

    • License

      • Select Continue

      • Select Agree

    • Installation Type

      • Select Install

      • Authenticate using your macOS password to install the software

      • Select Install Software

    • Summary; you should see that Node.js and npm were installed

      • Select Close

  3. Double-check that Node.js and NPM were installed:

You should now see output v12.18.0 or some other version of the software that's just been installed.

You should now see output 6.14.5 which represents the NPM version.

2) Using homebrew to install and update Node.js#

Homebrew is arguably the most popular package manager for macOS and makes installing Node.js straightforward. Let's check whether you have Homebrew installed:

If Homebrew is installed on your mac, you should see its version, for example, Homebrew 2.3.0. If not, you can install Homebrew with the following command:

Assuming that Homebrew is already installed, type:

And that’s all you need. Again, try node -v to confirm Node.js version and npm -v to confirm NPM version.

While using Homebrew for Node.js installation is very easy, it comes with one disadvantage:

Unfortunately, Homebrew has a habit of installing only the latest version of Node.js.

This could be a problem because sometimes applications require a certain version of Node.js to work. Having the flexibility of using specific versions can be an asset. To fix this problem, the best option to install Node.js is via nvm, which stands for Node Version Manager.


Node Version Manager, nvm, is a bash script to manage multiple active Node.js versions. It is easy to run with the following steps:

  1. Open new terminal window

  2. Run nvm install script

    • The script clones the nvm repository to ~/.nvm and adds the source lines to your shell profile, which is one the following: ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc. You can use either curl or wget, depending on what you have on your computer:

    • You can also add the source lines manually. Open your shell profile, which is one the following: ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc. Since I use zsh shell, I would do:

    • This command opens your shell profile. Then copy and paste the following:

    • Close the vim file by pressing: : + esc + w + q

  3. Reload the shell configuration by using the following command

  4. Verify the installation

  5. This should print nvm if the installation was successful.

  6. Use nvm to install Node.js. There are several commands you will find handy

    • List all command options by using: nvm + tab key

    • Download, compile, and install the latest release of node:

      Here node is an alias for the latest version

    • Install the specific version, for example v12.18.0:

    • Next, you actually have to use that specific version of Node that you just installed:

    • Print the path to the executable file where the specific version was installed:

    • Change the default Node.js version, let's say from 12.18.0 to 10.20.1:

    • Migrate packages from a previous Node.js version: nvm install node --reinstall-packages-from=node, for example:

    • Delete an older version of Node.js:

    • Install and use the LTS version:

    • List all installed Node.js versions available locally on your machine:

  7. Finally, use nvm to install NPM

    Please note that this installs the latest working NPM on the node version that you're currently using, such as ~/.nvm/versions/node/[your-version]/lib/node_modules/npm. This is something you want because you want to update the NPM and packages only for that Node.js version which is associated with a specific project and its requirements. Once you have NPM set up, there are several handy commands you can check out:

    • List globally installed packages:

    • Update all globally installed packages:

      Again, note that this will update packages inside the path of your active Node.js version instead of the system global path.

Congratulations, you have now successfully installed and understood the ins and outs of Node.js and NPM.

Here is what you have learned:#

  • You have seen the installation of Node.js via the mac installer

  • You also have seen the installation of Node.js via homebrew, arguably the most popular package manager for macOS

  • However, the best way to install Node.js is using Node Version Manager. This gives you additional control and flexibility when it comes to customizing different Node.js versions, which might be needed if you are hopping between different projects and their requirements.

I’ve helped you to explore some very basic commands, but please don’t stop here. It’s just the beginning and it’s up to you where this journey will take you!






Master full stack web development

Get access to every book and guide as a newline Pro member

Join now

Learn

Floating Button

Button