Beginner Fundamentals

Using npm

npm is the package manager that ships with Node.js. It downloads libraries (packages) from the registry and tracks them in your project.

Starting a project

Run npm init to create a package.json file. Use npm init -y to accept all defaults quickly.

// In the terminal
// npm init -y

Installing packages

Use npm install followed by the package name:

// npm install lodash
// Then use it in code:
const _ = require("lodash");
console.log(_.capitalize("hello")); // 'Hello'

Dependencies and node_modules

  • Installed packages go into the node_modules folder.
  • They are listed under dependencies in package.json.
  • Use --save-dev for tools only needed during development.
// npm install jest --save-dev

node_modules and version control

The node_modules folder can be huge. Do not commit it. Add it to .gitignore and run npm install to rebuild it from package.json.

Summary

  • npm init creates a project.
  • npm install adds packages.
  • package.json records what your project needs.