Node Package Manager (npm) is an indispensable tool for modern web development, serving as the default package manager for the Node.js JavaScript runtime environment. For Mac users embarking on their journey into web development, or for those looking to ensure they have the latest and most efficient setup, understanding how to install and manage npm is a fundamental step. This guide will walk you through the process, offering insights into the different methods and best practices for a seamless installation experience.
Understanding Node.js and npm
Before diving into the installation, it’s crucial to grasp the relationship between Node.js and npm. Node.js is a JavaScript runtime environment that allows developers to execute JavaScript code outside of a web browser. This capability has revolutionized server-side development, enabling the creation of scalable network applications, command-line tools, and much more.

npm, on the other hand, is bundled with Node.js. Its primary function is to manage project dependencies—the external libraries and packages that your Node.js applications rely on. It allows you to install, update, and remove these packages with ease, ensuring that your projects are built upon stable and well-maintained components. When you install Node.js, npm is automatically included, making the process of getting started with JavaScript development on your Mac remarkably straightforward.
The Role of npm in Development
npm acts as a central registry for open-source JavaScript packages. Developers worldwide contribute to this vast repository, offering solutions for everything from front-end frameworks like React and Vue.js to back-end utilities and build tools.
- Dependency Management: npm simplifies the process of managing project dependencies. Instead of manually downloading and integrating numerous libraries, you can simply specify the required packages and their versions in a
package.jsonfile. npm then handles the downloading and installation of these dependencies, ensuring consistency across different development environments. - Package Publishing: npm also provides a platform for developers to publish their own packages, sharing their creations with the wider community.
- Script Execution: The
package.jsonfile can also define scripts, allowing you to automate common development tasks such as building, testing, and deploying your applications with simple commands likenpm run buildornpm test.
Installation Methods for Node.js and npm
There are several ways to install Node.js and npm on a Mac, each with its own advantages. The most common and recommended methods involve using the official Node.js installer or employing a version manager.
Method 1: Using the Official Node.js Installer
This is arguably the simplest method for getting Node.js and npm up and running on your Mac. It involves downloading an installer package directly from the official Node.js website and running it like any other Mac application.
Steps for Using the Official Installer:
- Visit the Official Node.js Website: Navigate to https://nodejs.org/. You will typically see two download options: the “Recommended For Most Users” (LTS – Long Term Support) version and the “Current” version. For most development scenarios, the LTS version is recommended due to its stability and extended support.
- Download the macOS Installer: Click on the download link for the macOS installer. This will download a
.pkgfile to your computer. - Run the Installer: Locate the downloaded
.pkgfile (usually in your Downloads folder) and double-click it. - Follow the Installation Wizard: A standard macOS installation wizard will appear. Click “Continue” through the introductory screens, license agreement, and installation type.
- Select Installation Destination: You can usually accept the default installation location.
- Install: Click the “Install” button. You may be prompted to enter your administrator password to authorize the installation.
- Verify the Installation: Once the installation is complete, you can verify that Node.js and npm have been installed correctly by opening the Terminal application (you can find it in Applications > Utilities, or by searching with Spotlight).
- To check the Node.js version, type:
bash
node -v
- To check the npm version, type:
bash
npm -v
If the installation was successful, you will see the version numbers printed in the Terminal.
- To check the Node.js version, type:
Advantages:
- Extremely straightforward and user-friendly.
- No additional tools required beyond the installer itself.
- Suitable for beginners or those who only need a single, stable version of Node.js.
Disadvantages:
- Managing multiple Node.js versions can become cumbersome.
- Updating Node.js and npm requires re-downloading and running the installer.
Method 2: Using a Node Version Manager (NVM)
For developers who frequently work with different projects that may require specific versions of Node.js, a Node Version Manager (NVM) is an invaluable tool. NVM allows you to install and switch between multiple Node.js versions seamlessly. While there are a few NVM options for macOS, nvm (Node Version Manager) is the most popular and widely used.
Steps for Installing and Using nvm:
-
Install Homebrew (if you don’t have it): Homebrew is a package manager for macOS that simplifies the installation of software. If you don’t have Homebrew installed, open your Terminal and run the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Follow the on-screen instructions to complete the Homebrew installation.
-
Install
nvmusing Homebrew: Once Homebrew is installed, you can installnvmby running:brew install nvm -
Configure your Shell Profile: After installing
nvm, you need to add a few lines to your shell’s configuration file so thatnvmis loaded every time you open a new Terminal session.- For Zsh (the default shell on recent macOS versions), edit
~/.zshrc:
bash
nano ~/.zshrc
- For Bash, edit
~/.bash_profileor~/.bashrc:
bash
nano ~/.bash_profile
Add the following lines to the end of the file:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completionSave the file (Ctrl+X, then Y, then Enter in nano).
- For Zsh (the default shell on recent macOS versions), edit
-
Load
nvm: Close and re-open your Terminal, or run the following command to loadnvmin your current session:
bash
source ~/.zshrc # Or source ~/.bash_profile

-
Install Node.js Versions: Now you can use
nvmto install Node.js.- To install the latest LTS version:
bash
nvm install --lts
- To install a specific version (e.g., Node.js 18.17.0):
bash
nvm install 18.17.0
- To install the latest “Current” version:
bash
nvm install node
- To install the latest LTS version:
-
Use a Node.js Version: After installing a version, you need to tell
nvmto use it:nvm use --lts # To use the latest LTS version you installed nvm use 18.17.0 # To use a specific installed versionYou can also set a default version that
nvmwill automatically use when you open a new Terminal session:nvm alias default <version_number_or_alias>For example:
nvm alias default node # Sets the latest installed version as default -
Verify the Installation: Similar to the installer method, verify your Node.js and npm installations:
bash
node -v
npm -v
Advantages:
- Effortlessly manage multiple Node.js versions.
- Easily switch between projects requiring different Node.js environments.
- Streamlined updates and installations of new Node.js versions.
- Essential for professional developers working on diverse projects.
Disadvantages:
- Requires an initial setup involving Homebrew and shell configuration.
- Slightly more complex for absolute beginners.
Managing npm Packages
Once Node.js and npm are installed, you’ll spend a significant amount of time using npm to manage your project’s dependencies.
package.json
Every Node.js project that uses npm should have a package.json file. This file is the heart of your project’s configuration and dependency management.
-
Initialization: To create a
package.jsonfile in your project’s root directory, navigate to the directory in your Terminal and run:npm init -yThe
-yflag accepts all the default prompts, creating a basicpackage.jsonfile. You can runnpm initwithout-yfor a more interactive setup. -
Dependencies: When you install packages for your project, they are listed in the
dependenciessection ofpackage.json. These are packages required for your application to run in production.npm install <package-name>For example, to install the
lodashutility library:npm install lodashThis command will install
lodashand automatically add it to yourdependenciesinpackage.json, along with its own dependencies. -
Development Dependencies: Some packages are only needed during the development process (e.g., testing frameworks, build tools, linters). These are installed as dev dependencies.
bash
npm install <package-name> --save-dev
For example, to install thejesttesting framework:
bash
npm install jest --save-dev
This will addjestto thedevDependenciessection of yourpackage.json.
Common npm Commands
-
Installing all project dependencies: If you clone a project from a repository, you’ll need to install its dependencies. Navigate to the project’s directory in the Terminal and run:
npm installThis command reads the
package.jsonfile and installs all listed dependencies. -
Updating packages: To update all packages to their latest compatible versions as specified by
package.json:npm updateTo update a specific package:
npm update <package-name> -
Uninstalling packages: To remove a package from your project and
package.json:npm uninstall <package-name>To uninstall a dev dependency:
npm uninstall <package-name> --save-dev -
Running scripts: npm scripts defined in
package.jsonare executed usingnpm run.
bash
npm run <script-name>
For example, if you have atestscript defined, you would runnpm run test.

Best Practices for npm on Mac
-
Keep npm Updated: npm is regularly updated with new features, performance improvements, and security patches. You can update npm to its latest version using npm itself:
npm install -g npm@latestThe
-gflag installs the package globally, making it available system-wide. -
Understand Semantic Versioning (SemVer): npm uses SemVer (
MAJOR.MINOR.PATCH) to manage package versions. Understanding this system helps you manage dependencies more effectively and avoid breaking changes. -
Use
.npmrcfor Configuration: For more advanced npm configurations, you can create a.npmrcfile in your home directory or project directory to set registries, proxies, and other settings. -
Be Mindful of Global Installations: While global installations (
npm install -g) are useful for command-line tools, it’s generally best practice to install project dependencies locally within each project to avoid version conflicts and ensure project portability.
By following these guidelines and understanding the fundamental concepts, Mac users can efficiently install and manage npm, setting a strong foundation for their JavaScript development endeavors.
