NPM is a package manager that allows us to install and use 3rd-party Node libraries in our apps
Know how to install packages in an NPM project
Know what role the files package.json
, package-lock.json
, and the folder node_modules
play with NPM
We recommend you initialise your own NPM project to inspect the files we discuss on this page.
To create a new NPM project, create a folder, cd
inside and initialise a new NPM project in that folder with npm init -y
. The -y
flag accepts defaults by answering yes
to all setup questions.
package.json
If we created our NPM project as above, we should now see a single file in my-first-npm-project
: package.json
.
package.json
is the most important file in an NPM project because it specifies which packages our project depends on. When we install new packages, package.json
lists those packages' names and versions. When other SWEs clone our code on their own computers and need to run it, package.json
helps them install the exact packages and versions they need.
The following is the package.json
generated after running npm init -y
. Notice it has no packages yet, but contains other metadata for our project that is less relevant for us now.
After installing cows
, we should see 1 new file and 1 new folder: package-lock.json
and node_modules
respectively in addition to package.json
.
Feel free to peek at package-lock.json
and node_modules
, but their contents are primarily metadata for our app that we most likely never need to touch. package-lock.json
lists the versions of the packages we installed and of the packages that our installed packages depend on, aka "dependencies". node_modules
contains code of installed packages and their dependencies. NPM relies on all 3 of these files and folders to operate.
package.json
should now look like the following.
Notice there is a new dependencies
section that contains the package cows
and its version. Now, any time we clone a copy of this project and run npm i
from within the project folder, NPM will install the packages listed in this dependencies
section of package.json
.
First you will need to import (or require) the package and then invoke the cows() function to output the ASCII Cows. You may need to console.log the output of the function.
We should always commit both package.json
and package-lock.json
to Git for others that clone our project to have the same packages and versions.
We should avoid committing node_modules
to Git, because node_modules
can get large, and everything in node_modules
can be installed with npm i
. Committing large files like node_modules
to Git can unnecessarily slow down Git operations such as pulling and pushing from GitHub.
NPM packages are typically discovered via Google Search and tutorials. During Rocket's Bootcamp we will direct you to all packages we need, although Rocket encourages you to independently find new packages to enhance your projects, especially for your capstone.
(Node Package Manager) is Node's most popular package manager and allows us to install 3rd-party software packages (aka libraries) in our apps. All apps use 3rd-party libraries, and package managers like NPM simplify managing app dependencies.
To install NPM packages, run the install command npm install
, or npm i
for short, followed by the package name. The following command downloads and installs the , a package to create ASCII cow images.
Popular NPM packages almost always have clear instructions on how to use the package. We can typically find these instructions on the and on the .
When you are trying to implement the Cows package it will be very helpful if you visit the so you know the conventional usage of this package.