Node.js

What is the recommended way to install Nodejs nvm and npm on MacOS X

19 September 2026 · 10 min read

What is the recommended way to install Nodejs nvm and npm on MacOS X

Setting up your development environment on MacOS X can feel like navigating a labyrinth, especially when it comes to technologies like Node.js, npm (Node Package Manager), and nvm (Node Version Manager). Forget the days of haphazard installations leading to version conflicts and system-wide headaches. This guide will provide you with the recommended way to install Node.js, nvm, and npm on MacOS X, ensuring a smooth and efficient development experience. We’ll delve into best practices, step-by-step instructions, and troubleshooting tips to help you manage your Node.js versions like a pro. Proper management of your development environment avoids common pitfalls and future compatibility issues. By the end of this guide, you’ll have a clean, organized, and easily maintainable Node.js setup.

Why Use NVM to Install Node.js and npm?

Directly installing Node.js and npm using package managers like Homebrew or by downloading the installer can lead to versioning nightmares. Imagine working on multiple projects, each requiring a different Node.js version. Manually switching between these versions becomes incredibly cumbersome and error-prone. This is where nvm shines. NVM, or Node Version Manager, allows you to install and manage multiple active Node.js versions on your system. This means you can effortlessly switch between versions based on the specific needs of your projects. Think of it as a container system for Node.js – keeping each version isolated and preventing conflicts.

Using nvm provides several significant advantages. First, it allows you to easily test your code against different Node.js versions, ensuring compatibility across various environments. Second, it simplifies the process of upgrading or downgrading Node.js. No more uninstalling and reinstalling – just a simple command. Third, it prevents permission issues that can arise when installing Node.js globally using other methods. Nvm installs Node.js versions within your user directory, eliminating the need for administrator privileges. According to a Stack Overflow Developer Survey, over 70% of professional developers use package managers like npm, highlighting the importance of understanding these tools [^1^].

The flexibility and control offered by nvm make it the recommended way to install Node.js, nvm, and npm on MacOS X for any serious Node.js developer. It’s a small investment in time that pays off significantly in terms of productivity, stability, and reduced debugging efforts. By using NVM, you prevent yourself from encountering common problems like “permission denied” errors when installing global packages, or conflicts between different versions of Node.js that your projects may depend on.

Step-by-Step Guide to Installing NVM, Node.js, and npm

Now, let’s dive into the installation process. Here’s a detailed, step-by-step guide to get you up and running with nvm, Node.js, and npm on your MacOS X system:

  1. Install Homebrew (if you don’t have it): Open your terminal and run the following command: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" Homebrew is a package manager for MacOS that simplifies the installation of other tools.
  2. Install nvm: Use Homebrew to install nvm: brew install nvm
  3. Configure your shell: After installing nvm, you need to configure your shell (e.g., bash or zsh) to use it. Add the following lines to your ~/.bashrc, ~/.zshrc, or ~/.profile file: export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" This loads nvm You can use a text editor like nano or vim to edit these files.
  4. Reload your shell: After modifying your shell configuration, reload it by running: source ~/.bashrc (or source ~/.zshrc or source ~/.profile, depending on your shell).
  5. Install Node.js using nvm: Now you can install specific Node.js versions. For example, to install the latest LTS (Long Term Support) version, run: nvm install --lts To install a specific version, run: nvm install 16.13.0 (replace with the desired version).
  6. Use a Node.js version: After installing a version, you need to tell nvm to use it. Run: nvm use --lts (or nvm use 16.13.0, depending on the version you installed).
  7. Verify the installation: Finally, verify that Node.js and npm are installed correctly by running: node -v and npm -v. These commands should display the installed versions.

Following these steps ensures a clean and manageable Node.js environment on your MacOS X system. Remember to reload your shell after making changes to your shell configuration files. This is a crucial step often overlooked, leading to nvm not being recognized by the system.

Common Issues and Troubleshooting

Even with detailed instructions, you might encounter some common issues during the installation process. Let’s address some of these:

  • “nvm command not found”: This usually means that nvm is not properly sourced in your shell configuration file. Double-check that you’ve added the correct lines to your ~/.bashrc or ~/.zshrc file and that you’ve reloaded your shell.
  • Permission errors when installing global packages: If you’re encountering permission errors when using npm install -g, it’s likely because you’re trying to install packages globally without proper permissions. Nvm is designed to avoid this, but if it persists, ensure that the nvm directory and the Node.js versions installed within it are owned by your user account.
  • Conflicting Node.js installations: If you previously installed Node.js using another method (e.g., Homebrew), it might conflict with nvm. Consider uninstalling the previous installation to avoid conflicts. You can usually do this with: brew uninstall node.

For example, consider a scenario where a developer is working on a legacy project requiring Node.js version 12 and a new project requiring Node.js version 16. Without nvm, managing these dependencies becomes a logistical nightmare. With nvm, they can simply switch between versions using nvm use 12 and nvm use 16, ensuring that each project runs with the correct dependencies. Remember to always check for potential conflicts with existing installations. This can save you a lot of time and frustration in the long run.

One key tip for troubleshooting is to carefully read the error messages. They often provide valuable clues about the underlying problem. Search online forums and communities for similar issues – chances are someone else has encountered the same problem and found a solution. The Node.js and npm communities are very active and helpful.

Best Practices for Managing Node.js with NVM

Once you have nvm, Node.js, and npm installed, it’s important to adopt best practices to keep your development environment clean and organized.

First, always specify the Node.js version for your projects using a .nvmrc file. This file, placed in the root directory of your project, tells nvm which Node.js version to use when you navigate to that directory. The file should simply contain the Node.js version number (e.g., 16.13.0 or lts/ for the latest LTS version). When you enter the project directory, nvm will automatically switch to the specified version. This ensures that everyone working on the project uses the same Node.js version, preventing compatibility issues.

Second, keep your global npm packages to a minimum. Installing too many packages globally can lead to conflicts and inconsistencies. Instead, prefer installing packages locally within your project using npm install --save or npm install --save-dev. This creates a node_modules directory in your project and adds the packages to your package.json file, making your project self-contained and reproducible. This approach also makes it easier to manage dependencies and avoid version conflicts.

Third, regularly update your Node.js versions using nvm. Keeping your Node.js versions up-to-date ensures that you benefit from the latest security patches, performance improvements, and new features. Use nvm install --lts to install the latest LTS version or nvm install node to install the latest stable version. Remember to test your code after updating to ensure compatibility. According to the Node.js Foundation, staying current with Node.js releases significantly reduces security vulnerabilities [^2^]. The following paragraph is optimized for a featured snippet:

To effectively manage your Node.js versions using NVM, create a .nvmrc file in your project’s root directory specifying the required Node.js version. This ensures consistency across development environments. Include the version number (e.g., 16.13.0) or lts/ for the latest Long Term Support version. When you navigate to the project directory, NVM automatically switches to the specified Node.js version, streamlining your workflow and preventing compatibility issues. This simple step significantly enhances project maintainability and collaboration.

Here’s a summary of key best practices:

  • Use a .nvmrc file to specify the Node.js version for each project.
  • Minimize global npm packages.
  • Regularly update your Node.js versions.
Infographic here
By following these best practices, you'll create a robust and manageable Node.js development environment on your MacOS X system, reducing the risk of errors and improving your overall productivity. Remember that consistency is key – strive to maintain a consistent development environment across all your projects.

FAQ: Installing Node.js, nvm, and npm on MacOS X

**Q: What is the best way to install Node.js on MacOS X?**
A: The recommended way is to use Node Version Manager (nvm). Nvm allows you to install and manage multiple Node.js versions on your system, preventing conflicts and simplifying version switching.
**Q: How do I check if nvm is installed correctly?**
A: Open your terminal and run the command `nvm --version`. If nvm is installed correctly, it will display the nvm version number.
**Q: How do I switch between Node.js versions using nvm?**
A: Use the command `nvm use [version_number]`, replacing `[version_number]` with the desired Node.js version (e.g., `nvm use 16.13.0`).
**Q: How do I set a default Node.js version using nvm?**
A: Use the command `nvm alias default [version_number]`, replacing `[version_number]` with the desired default Node.js version (e.g., `nvm alias default 16.13.0`).
**Q: What if I get a "permission denied" error when installing global npm packages?**
A: This usually happens when npm tries to install packages globally without proper permissions. Ensure that the nvm directory and the Node.js versions installed within it are owned by your user account.
For further reading, consult the official Node.js documentation \[^3^\], the nvm GitHub repository, and community forums for more detailed information and troubleshooting tips. These resources provide valuable insights and can help you resolve any issues you might encounter.

Installing and managing Node.js, npm, and nvm on MacOS X doesn’t have to be daunting. By following these steps and best practices, you can create a clean, organized, and efficient development environment. Remember the power of NVM in allowing you to manage different versions and prevent a host of problems down the line. So, take action now! Configure your environment correctly using this guide and start building amazing applications with Node.js. To further enhance your knowledge, consider exploring topics like package.json best practices, CI/CD pipelines for Node.js applications, and performance optimization techniques. Check out our other articles to learn more.

[^1^]: Stack Overflow Developer Survey: [Homebrew](<https://insights. Question & Answer :

I am trying to use <a href=>) as much as possible. What’s the recommended way to install Node.js, nvm and npm on MacOS X?

  1. Using homebrew install nvm:

    brew update brew install nvm source $(brew --prefix nvm)/nvm.sh 
    

    Add the last command to the .profile, .bashrc or .zshrc file to not run it again on every terminal start. So for example to add it to the .profile run:

    echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.profile 
    

    If you have trouble with installing nvm using brew you can install it manually (see here)

  2. Using nvm install node or iojs (you can install any version you want):

    nvm install 0.10 # or nvm install iojs-1.2.0 
    
  3. npm is shipping with node (or iojs), so it will be available after installing node (or iojs). You may want to upgrade it to the latest version:

    $ npm install -g npm@latest 
    

    UPD Previous version was npm update -g npm. Thanks to @Metallica for pointing to the correct way (look at the comment bellow).

  4. Using npm install ionic:

    npm install -g ionic 
    
  5. What about ngCordova: you can install it using npm or bower. I don’t know what variant is more fit for you, it depends on the package manager you want to use for the client side. So I’ll describe them both:

    1. Using npm: Go to your project folder and install ng-cordova in it:

      npm install --save ng-cordova 
      
    2. Using bower: Install bower:

      npm install -g bower 
      

      And then go to your project folder and install ngCordova in it:

      bower install --save ngCordova 
      

PS

  1. Some commands may require superuser privilege
  2. Short variant of npm install some_module is npm i some_module