← Back to Node.js Course | Chapter 1: Introduction | Lesson 7 of 7

npm basics

npm is Node's package manager: it downloads other people's code and tracks the dependencies of your project.

In this page:

  1. npm basics
Syntax
javascript
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

bash
$ 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
  1. Committing node_modules to git
  2. Editing package-lock.json by hand
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.