What npm is and why you need it on your Mac

npm is a package manager for JavaScript — it downloads and installs code libraries that other developers have written, so you don't have to build everything from scratch. When you're working on a JavaScript project, npm handles the tedious work of fetching the right versions of those libraries and keeping them organized in a folder called node_modules.

Before you can use npm, you need to install Node.js, the JavaScript runtime that npm comes bundled with. On a Mac, you have two main routes: download an installer from nodejs.org, or use Homebrew if you already have it. Both take about five minutes.

Key Takeaways

  • npm comes automatically when you install Node.js, so you don't install them separately.
  • The easiest route for most people is downloading the installer from nodejs.org and running it like any other Mac app.
  • If you use Homebrew, the command brew install node installs both Node.js and npm at once.
  • After installation, run node --version and npm --version in Terminal to confirm both are working.
  • npm stores downloaded packages in a node_modules folder inside your project, not system-wide.

Installing Node.js and npm using the official installer

Go to nodejs.org and download the LTS (Long Term Support) version — it's the most stable choice for learning and most projects. The site detects your Mac automatically, so you'll get the right file for your processor (Intel or Apple Silicon). The file downloads as a .pkg installer.

Double-click the .pkg file to open the installer. Follow the prompts: agree to the license, choose your installation location (the default is fine), and enter your Mac password when asked. The installer places Node.js and npm in a system folder so Terminal can find them from anywhere.

When the installer finishes, open Terminal (search for it in Spotlight or find it in Applications > Utilities). Type node --version and press Enter. You should see a version number like v20.10.0. Then type npm --version and press Enter. You should see something like 10.2.3. If both commands show version numbers, npm is ready to use.

Installing Node.js and npm using Homebrew

If you already use Homebrew to manage software on your Mac, installing Node.js through it keeps everything in one place. Open Terminal and type brew install node, then press Enter. Homebrew downloads and installs both Node.js and npm automatically.

After the installation finishes, verify it worked the same way: type node --version and npm --version in Terminal. Both should return version numbers. If you see command not found errors, close Terminal completely and open a new window — sometimes Terminal needs to reload its settings after a Homebrew install.

If you don't have Homebrew installed yet, you can install it from brew.sh. The process takes a few minutes and requires your Mac password.

Checking your npm configuration and default location

After npm is installed, it creates a hidden folder in your home directory called .npm where it stores some configuration. You don't need to touch this folder — npm manages it automatically. When you start a project and run npm install, npm creates a node_modules folder inside that specific project folder, not in a system-wide location.

To see where npm is installed, type which npm in Terminal. You'll see a path like /usr/local/bin/npm (Intel Mac) or /opt/homebrew/bin/npm (Apple Silicon with Homebrew). This tells you npm is in your system's executable path, so Terminal can find it from any folder.

Updating npm after installation

npm updates more frequently than Node.js does. If you installed Node.js recently, npm is probably current, but you can check by typing npm --version and comparing it to the latest version listed at npmjs.com. If an update is available, type npm install -g npm@latest to upgrade npm itself.

The -g flag means "global" — it installs npm in a system location so all your projects can use the newest version. This is one of the few times you'll use the -g flag; most packages you install should go into individual projects, not globally.

Troubleshooting common installation problems

If Terminal says "command not found" after installation, the most common cause is that Terminal hasn't reloaded its path settings. Close Terminal completely (Command+Q) and open it again. If that doesn't work, restart your Mac.

If you installed Node.js but npm still isn't found, check whether you have multiple versions of Node.js installed. Type which node to see which one Terminal is using. If you installed both via the official installer and Homebrew, they may be conflicting. Uninstall one (the official installer has an uninstall script in /usr/local/bin; Homebrew uses brew uninstall node) and reinstall using only one method.

On Apple Silicon Macs, make sure you downloaded the correct version from nodejs.org — the site should detect your processor, but if you accidentally downloaded the Intel version, it will still install but may cause issues. Check by typing node -p process.arch in Terminal; you should see arm64 on Apple Silicon or x64 on Intel.

Your first npm project

Once npm is working, create a folder for a test project. In Terminal, type mkdir my-first-project to create a folder, then cd my-first-project to enter it. Type npm init -y to create a package.json file — this is the file npm uses to track which packages your project needs.

Now try installing a real package. Type npm install lodash to download a popular utility library. npm creates a node_modules folder and downloads lodash into it. Open the folder in your file explorer and you'll see the node_modules folder and a package-lock.json file. These files tell npm exactly which versions of which packages your project uses, so other people can install the same versions by running npm install in the same folder.

Frequently Asked Questions

Do I need to install npm separately from Node.js?

No. npm comes automatically when you install Node.js. You only run one installer or one Homebrew command, and both arrive together. There's no separate npm installer to download.

Should I use the official installer or Homebrew?

Either works. The official installer is simpler if you don't use Homebrew for other things. Homebrew is better if you already use it to manage other software, because it keeps everything in one place and makes updates easier. Pick whichever fits your workflow.

Why does npm create a node_modules folder in every project?

Each project can need different versions of the same package. Storing packages inside each project folder keeps them separate and prevents conflicts. It also makes your project portable — you can move the folder to another computer and run npm install to get the same packages.

Is it safe to delete the node_modules folder?

Yes. The folder is just a cache of downloaded packages. As long as you keep your package.json and package-lock.json files, you can delete node_modules and run npm install again to rebuild it. This is useful if the folder gets corrupted or takes up too much disk space.

What's the difference between npm install and npm install -g?

Without -g, packages install into your current project's node_modules folder. With -g, they install system-wide so all projects can use them. Use -g only for command-line tools you run from Terminal; use the default for libraries your code imports.