Introduction to Node.js - NPM
Introduction to Node.js - NPM

This course offers an introduction to the NPM, Node Package Manager. NPM allows for the installation of third-party node modules to be used in your project. NPM is also an online registry for third-party node modules.

Learning Objectives

  • Learn how to initialize a Node project using NPM
  • Learn how to install a third-party module using NPM
  • Learn the difference between dependencies and dev dependencies
  • Learn how to write and modify existing scripts in the package.json file
  • Learn how to install packages from an existing package.json file

Intended Audience

This course is intended for anyone who wants to learn about Node.js.


There are no prerequisites for this course but an interest in Node.js would be beneficial.



Introduction to Node.js - NPM. NPM which stands for Node Package Manager is one of the many package managers available for node. NPM is included with the installation of node. NPM is also an online registry for node packages. This allows developers to create packages and share them with other developers. In order to use these packages and for other use cases, NPM requires a package file that is written in JSON to be included in your node project. Now back at the lab's environment, I have the terminal open. In order to create this file, I need to use the NPM init command. In the terminal, I will type npm init and this will trigger a series of questions. The package name is for the name of the project.
For this demonstration, I will type calabs. The version number is for creating a version history. I will leave it at version one. In order to use the default values that are generated by the init command, I just need to hit enter. For the majority of these, I will be using the given default value. The description is for a description of the project. The entry point is the main file where the execution starts. Since I currently don't have any files, I will leave this at the default. The test command is the command needed in order to execute a testing framework. I will leave this blank for now, but I will circle back to this later on in the video. The Git repository is for the remote repository. Keywords is for the NPM registry search engine to make it easier for other developers to find your package. Author is for the company or individual or group of developers that created this project, I will put in my name. License is for the terms of use of your package. I will use the MIT license and hit 'Enter.' And in the terminal's output is the data that will appear in the package JSON file. Below I'm asked if this is okay, and I will hit 'Enter' since the default is yes. I will open up the package.json file in the editor.

Before continuing any further, I'm going to go back to the NPM registry page. For this video, there are three packages I will be installing: chalk, prompts, and jest. Using chalk as an example of how to navigate through the registry, in the search bar I will type chalk. I will select on this first result and there's a lot of information here on the package. Listed here is the weekly downloads and the repository links. Scrolling down a little further is a guide on how to use this package. Now, sometimes these guides aren't up to date or do not have that much information. From personal experience, I usually find better package instructions located directly in the repository. Here are the installation instructions for the package. I will now return back to the lab environment. Before going further, one thing to note, this is a video on how to use NPM and not a video on how to use the packages being used for this video. In the terminal, I will type npm install chalk. And now the package is being downloaded from the registry. After completion, the terminal now has some information on the installation of chalk. I will repeat this process to install the prompts package. The package.json file has refreshed and there's some new information here. There is a property called dependencies, and inside of it both of the chalk and prompts package are listed along with their version numbers. Now that they've been installed, these packages are available to use throughout the project. Packages that are listed under dependencies are expected to be used in production and development. There are packages that are meant to be used for development only. I'm going to install the JavaScript test framework jest. In the terminal, I will type npm install --save-dev jest and I will hit 'Enter.' This package is much larger than the previous two, so I will speed up the installation process.
Now in the package.json file, there is a new property called devDependencies which currently lists jest. This only happened because I used the save dev flag. Otherwise, jest would be listed under regular dependencies. Packages that are listed under devDependencies are excluded from the application when the application is built for production or deployment. Now, an important aspect of the package JSON file is the scripts section. This section allows developers to create command shortcuts and automation options for a startup. Earlier, when I initialized the package, it asked me for a test command which I left blank. And the current message listed under test is the default when one is not given. And the reason why I installed jest was for demonstration purposes, and the execution command for jest is jest. So, in order for me to set up a test command, I will change this message to jest. Now in order to execute a script, I have to type NPM in the script name. So, in the terminal, I will type npm test. And the terminal shows jest being executed. And it comes back with a message, 'No tests found,' which is expected.
Now, I can expand upon the scripts and add as many scripts as I like. To demonstrate this, I'm going to create an index.js file, that entry point that was given earlier in the video. In the file, I'm going to console.log, start script. Going back to the package JSON file, under scripts, I will add a new property called start and I will give it a value of node index.js. So, what I'm indicating here is that when I type npm start, I want the node command to be executed on the entry point, the index file. In the terminal, I will type npm start and hit 'Enter.' And start script appears in the terminal.
So, the package.json file serves a few purposes, and I will cover one more use case for this video. To the left, there is this node_modules folder. I'm going to expand it and there are many subfolders here. Inside of these subfolders are more folders and files. This folder is initially created when the first package is installed. As additional packages are installed, their corresponding files and folders go into this folder. It is not considered a best practice to include these folders in your remote repository. That package.json file accessing multiple tracking system, it lists the packages and their version numbers currently used for this project. When somebody is working off a clone or fork of your repository, they can quickly get up and running by using this file. To demonstrate this, I will delete the node_modules folder. Now that the folder is gone, I will type npm install. In the terminal, it lists that 352 packages were installed in two seconds. Now, this was most likely faster than normal because there's probably some caching involved with the recently deleted node modules. And to the left, the node_modules folder has reappeared. And that's it, thanks for watching at Cloud Academy.

About the Author
Learning Paths

Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.

Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.

Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.

Farish is an avid powerlifter, sushi lover, and occasional Funko collector.