How to Install Node.js and NPM on Windows

UC Berkeley Extension: Using Command Line and Javascript learn how to install Node.js on Windows using our detailed online tutorial involving NPM and Json.

​​​​​​Node.js and NPM are essential to using JavaScript development tools, such as Webpack, Gulp, and Grunt. These three are among the most popular technologies, and you’ll need a working knowledge of the Node system to use them.

First, and this is especially important, make sure you know the basics of how to use the command line. If you don’t know how to use the command line, you’re going to have a harder time installing and using either node.js or NPM. A great way to learn is earning a JavaScript certificate of completion, which will teach you the language’s essentials. With that said, let’s go over what node.js and NPM actually are.

Node.js

Node.js is an open-source JavaScript run-time environment that is cross-platform and executes JavaScript outside browsers. Node.js allows JavaScript to be used as a server-side programming language. If you don’t learn node.js, JavaScript is limited to being used as a client-side language and can only be processed in browsers. Using node.js greatly widens the applicability of JavaScript. Node.js allows JavaScript to be used for command line tools and server-side scripting. This means that node.js enables JavaScript to create server-side scripts to make dynamic webpages before the user’s browser actually receives the page.

A major benefit of learning node.js is it allows JavaScript to act as a universal language encompassing both the server-side and client-side. Node.js also has an event-driven architecture, meaning it’s capable of asynchronous I/O. Essentially, when you learn node.js, you can provide scalability and throughput in applications, whether input/output or real-time.

NPM

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.

NPM is a recommended feature in the node.js installer. NPM lets you use and distribute JavaScript modules available in its registry. (Currently, there are just under 500,000 packages available.)

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:

NPM Install

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)
  • Description

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:

leftPad (“String”, 10)

then the output will be:

console String

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

Conclusion

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.

At this point, you’re prepped to start using Webpack, Grunt, Browseify, or anything else that uses node.js and NPM. If you want to learn more, consider obtaining a JavaScript certificate of completion by attending an online coding bootcamp. A JavaScript certificate of completion can help make you more comfortable working with both node.js and NPM.

Source: UC Berkeley Extension

Share:


Tags: coding bootcamp, command line, javascript certificate, Json, node.js, NPM