Packages
It is not productive to create everything from scratch. To increase productivity, NodeJS provides a mechanism to re-use previously written code. Such code is called a package. Packages can be installed via a commandline utility called NPM.
Packages can be installed either Global or local. Global packages are installed on the machine and can be used from the command line or by all Node apps from that single installation. Local packages are installed with each Node app and cannot be used by other Node apps.
When a Node app uses a package, it is called a dependency. This means that the Node app depends on the package in order to function properly. Packages can also use other packages as dependencies. Packages have version numbers. A version number indicates a specific iteration of code or features that the package supports. When a Node app depends on a specific version of a package, it avoids protentional problems and conflicts that might arise if there was no versioning in place. A conflict is when the Node app expects a certain feature or behavior that does not exist, or behaves differently than expected.
To install a package:
NodeJS provides a utility called npm
that manages packages. With it you can install and remove packages easily.
Each application should have a package.json
file in its root directory. This file does not exist by default and must be created. To create it, simply type:
npm init
at the application root directory and follow the prompts. To add a NodeJS package, simply type:
npm install <name> --save
at the application root directory.
You can also remove a package via:
npm remove <name>
at the application root directory.
When you get a project from GitHub the first time, it will usually contain the package.json
file already. But the packages will usually not be included with the git repository. The packages will be listed in the package.json
file. To install all the package dependencies, simply type:
npm intall
at the application root directory.
Some packages will behave more like applications and others more like functionality for YOUR Node app. Those that behave more like applications (or utilities) must be installed globally and those that provide functionality as a part of YOUR Node app must be installed locally. To install a global package, simply type:
npm install -g <name>
anywhere.
Exercises:
Create a new Node app. Add a
package.json
fileWhat is the purpose of the
main
key/value in thepackage.json
file?Install at least one package to the project
What happens if you omit
--save
when installing a package?Install at least one package that is specific to the dev dependencies (HINT: testing usually will be dev specific (as in development, vs deployed to the end users and ready to use))
Remove at least one package from the project
Validate that
package.json
reflects the change. If not, figure out why and try again
Install a specific version of a package to the project
What directory does NPM install local packages to?
What happens if you install a package at a different directory level than the application root directory?
How can you avoid uploading package files to github? (HINT: git.ignore... but how, specifically?)
Install a global package (
Nodemon
??)Can a global package be a dependency to your Node app? Explain why or why not?
Launch your NodeJS app using the dev configuration.
Last updated
Was this helpful?