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_modulesfolder. - They are listed under
dependenciesinpackage.json. - Use
--save-devfor 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 initcreates a project.npm installadds packages.package.jsonrecords what your project needs.