NodeJS + NPM
Contents
• What & Why?
• Installing NPM
• Packages
• Package.json
• Package-lock.json
• Useful commands
• Other package managers
• DO’s and DON’ts
What & Why?
• NPM = Node Package Manager
• It installs the packages you want to use and provides a
useful interface to work with them.
Installing NPM
1. Install NodeJS
2. That’s it
Packages
Packages
• Tools built using NodeJS
• Can be installed globally (user folder - default) or
locally (project folder)
• Can be fetched from the NPM Registry or a custom
repo (Git)
Packages
Installing packages globally
• npm install <package_name> —global
• May install other packages (dependencies)
Packages
Installing packages locally
• You need a package.json file
• npm init
• Go through the wizard
Init wizard
You can use npm init —y to bypass the wizard
Packages
Installing packages locally (cont.)
• npm install <package_name>
Example
• npm install underscore
Output
Package.json
Package.json
• Used for portability
• Committed to repository
• Every package is added as a property of the
dependencies field (or devDependencies)
Package.json
• ^ = most recent major version (anything up to 2.0.0)
• ~ = most recent minor version (1.9.x)
• @ = specific version (@1.9.1)
• * = any version
• Other -> check the docs
Package.json
DevDependencies
• Use the —saveDev flag when installing
• Packages used for development purposes, for example
for running tests or transpiling code
Package-lock.json
Package-lock.json
• Introduced in NPM v5
• Ensures that the dependencies remain the same on all
machines the project is installed on
• It should be committed along with package.json
Other useful commands
Other commands
• Npm I -g <package> -> install
• Npm un <package> -> uninstall
• Npm up -> updates all packages
• Npm i <package1> <package2> - installs multiple
packages at once
Other package managers
Other PM
• Bower - old, deprecated by the creators
• Yarn - higher performance, similar to NPM
DO’s and DON’ts
DO’s and DON’Ts
• DO commit both package.json and package-
lock.json to the project repository
• DON’T commit the node_modules folder
• DO pay attention to the dependencies of the package
you want to install
• DON’T install test runners or code transpilers as
dependencies (use —saveDev for that)
• DO use the scripts property in the package.json to
simplify your commands
Let’s use NPM