NPM
NPM (Node Package Manager) is the tool we use to download and manage the libraries our projects depend on. Instead of hunting down every library by hand, NPM reads a list of what the project needs (a file called package.json) and installs all of it for you with one command.
NPM doesn’t get installed on its own. It comes bundled with Node.js, which is the program that runs JavaScript outside of a web browser. So to get NPM, we install Node.js.
1. Download Node.js
Section titled “1. Download Node.js”Go to the Node.js download page and download the Windows Installer (.msi).
You’ll see two kinds of versions offered:
- LTS (Long Term Support): the stable version. Pick this one.
- Current: the newest features, but less tested.
Make sure the architecture matches your computer. For almost every modern Windows machine, that’s x64. If you’re not sure, press your Windows key, search for About your PC, and look at the System type line.
2. Run the Installer
Section titled “2. Run the Installer”Once it has been downloaded, open the .msi file from your Downloads folder and follow the prompts:
- Click Next on the welcome screen.
- Accept the license agreement and click Next.
- Leave the install location as the default and click Next.
- On the Custom Setup screen, leave everything as it is. Make sure npm package manager and Add to PATH are both included (they are by default). Click Next.
- On the Tools for Native Modules screen, leave the box unchecked and click Next. We don’t need these, and they take a long time to install.
- Click Install. If Windows asks whether you want to allow the app to make changes to your device, click Yes.
- Click Finish once it’s done.
3. Check That It Worked
Section titled “3. Check That It Worked”If you had a Command Prompt window open from before, close it and open a new one. Command Prompt only picks up newly installed programs when it starts.
Open the command line by pressing your Windows key and searching for cmd, then run the application called Command Prompt.
First, check that Node.js is installed:
node --versionIt should print out a version number, something like v22.20.0.
Next, check that NPM is installed:
npm --versionIt should print out another version number, something like 10.9.3. The exact numbers don’t matter, as long as you get a number and not an error.
4. Take It for a Test Drive
Section titled “4. Take It for a Test Drive”Seeing a version number means it’s installed, but let’s make sure it actually works.
Run some JavaScript
Section titled “Run some JavaScript”Node.js can run JavaScript right from the command line:
node -e "console.log('Hello from Node!')"It should print Hello from Node!.
Make a test project
Section titled “Make a test project”Let’s create a throwaway project and install a package into it. First, make a new folder and move into it:
mkdir npm-testcd npm-testCreate a new project. The -y just says “yes” to all the default settings:
npm init -yThis creates a package.json file, which is the list of everything your project depends on. It will print the contents of that file to the screen.
Now install a package. We’ll use cowsay, a small (and very important) package that makes a cow say things:
npm install cowsayWhen it finishes, you’ll see a message like added 41 packages. You’ll also notice a new node_modules folder in your project. That’s where NPM puts all the downloaded code.
Finally, run it:
npx cowsay "I installed NPM!"npx runs a package that’s installed in your project. If a cow shows up saying your message, everything is working.
__________________< I installed NPM! > ------------------ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||You can delete the npm-test folder once you’re done. We won’t need it again.
Troubleshooting
Section titled “Troubleshooting”'node' is not recognized as an internal or external command
Section titled “'node' is not recognized as an internal or external command”Windows can’t find Node.js yet. Close every Command Prompt window and open a new one. If that doesn’t fix it, restart your computer. If it still doesn’t work, run the installer again and make sure Add to PATH is included on the Custom Setup screen.
npm.ps1 cannot be loaded because running scripts is disabled on this system
Section titled “npm.ps1 cannot be loaded because running scripts is disabled on this system”This only happens in PowerShell (including the terminal inside VS Code), not in Command Prompt. Windows blocks PowerShell scripts by default. To allow them for your user account, open PowerShell and run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserType Y and press Enter if it asks you to confirm. You can read more about what this does in Microsoft’s documentation on execution policies.
