npm basics
npm is Node's package manager: it downloads other people's code and tracks the dependencies of your project.
In this page:
Syntax
npm init
npm install package_name
npm run script_name
npm basics
npm init creates a package.json describing your project. npm install <name> downloads a package into node_modules and records it in package.json. npm run <script> runs commands defined in the scripts section.
Note:
Commit package.json and package-lock.json, never node_modules.
Example: npm basics
$ npm init -y
$ npm install lodash
added 1 package in 2s
$ npm run
Scripts available in demo via `npm run-script`:
start
node index.js
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Committing node_modules to git
- Editing package-lock.json by hand
- Installing global packages when a local one is safer
Chapter Summary
- npm init creates package.json
- npm install adds dependencies
- npm run runs scripts
- Do not commit node_modules
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: