How to Install Node.js and NPM on Windows
Most people colloquially refer to NPM as the “node pack manager” because that’s essentially what it does. (NPM actually stands for “Node.js Package Manager.”) It’s the tool that lets you connect to a registry that has the Node.js modules, programs, and plugins, so it’s extremely useful.
NPM is the default package manager for Node.js. NPM is made up of a command line client (called NPM as well) and an online database of paid-for packages (public and private). This database is called the NPM registry. The NPM client allows you to access the NPM registry, and you can search available packages through the NPM website.
Global & Local
You’re probably used to globally installing a program or software on your computer, but this isn’t necessarily the case with NPM. NPM has some global installations, but most of the installs will be on a local project basis. What this means is you’ll need to install whatever is needed for every project in its own directory. This means you’ll need a new NPM install every time you create a new project. Global installations have the -g flag.
Installing Node.js and NPM on Windows
The Node.js and NPM installations on Windows are relatively simple. You can install both from a download link. First, go to the Node installation page and download the installer. Choose the operating system you’re using, and then go from there. Once the installer is finished, you can test to see NPM and node functioning. Do this by opening any shell and typing node -v and npm -v. This will check the version number.
Creating Your First Project
Now that you’ve installed Node.js and NPM, you’re ready to set up Webpack, Gulp, etc. You’re also able to create a project to make sure everything is working right.
First, navigate to the directory you want to place your project in. For example, place it in cd sites/note-test. Then, initialize a new project using NPM by entering:
At this point, you’ll receive a popup that says: “This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See npm help json` for definitive documentation on these fields and exactly what they do. Use ‘npm install’ afterwards to install a package and save it as a dependency in the package.json file.”
The utility will ask you for:
- Package name (node-test)
- Version number (1.0.0)
As an example project, you can write:
Creating my first “grand opening!” Node project.
What comes next can be passed over, so skip it for now. You’ll receive a notification that a package.json file containing the information you entered now exists.
A package.json file contains metadata about your project. It also handles the project’s dependencies (modules and software).
Your First Dependency
At this point, you’re ready to install the first dependency. We’ll start with a package called left-pad that will add white space to the left side of a string to add up to a number.
If you write:
then the output will be:
Left-pad is a package available on the NPM registry. So, to install a dependency like left-pad on NPM, use the command:
npm install dependency-name-here
While running npm install will download the dependency, it won’t save it to the project. Use the flag –save to install this dependency and add it to your package.json. Since you ran the command inside the project directory, it will install the dependency by forming a node_modules directory. For our purposes, you can skip the package-lock.json file that was also created.
At this point, your project recognizes that the left-pad dependency exists. To ensure that dependency is only used for development purposes, you can run:
npm install –save-dev
So, now you know what Node.js and NPM are and how to install them on Windows. You also know the difference between a local and global project, and how to make a local project. You know how to install an NPM dependency and run a file using node_modules in a shell.