Node.js — An introduction to the npm package manager (2024)

Introduction to npm

npm is the standard package manager for Node.js.

In September 2022 over 2.1 million packages were reported being listed in the npm registry, making it the biggest single language code repository on Earth, and you can be sure there is a package for (almost!) everything.

It started as a way to download and manage dependencies of Node.js packages, but it has since become a tool used also in frontend JavaScript.

Yarn and pnpm are alternatives to npm cli. You can check them out as well.

Packages

npm manages downloads of dependencies of your project.

Installing all dependencies

If a project has a package.json file, by running

npm install

it will install everything the project needs, in the node_modules folder, creating it if it's not existing already.

Installing a single package

You can also install a specific package by running

Furthermore, since npm 5, this command adds <package-name> to the package.json file dependencies. Before version 5, you needed to add the flag --save.

Often you'll see more flags added to this command:

  • --save-dev installs and adds the entry to the package.json file devDependencies
  • --no-save installs but does not add the entry to the package.json file dependencies
  • --save-optional installs and adds the entry to the package.json file optionalDependencies
  • --no-optional will prevent optional dependencies from being installed

Shorthands of the flags can also be used:

  • -S: --save
  • -D: --save-dev
  • -O: --save-optional

The difference between devDependencies and dependencies is that the former contains development tools, like a testing library, while the latter is bundled with the app in production.

As for the optionalDependencies the difference is that build failure of the dependency will not cause installation to fail. But it is your program's responsibility to handle the lack of the dependency. Read more about optional dependencies.

Updating packages

Updating is also made easy, by running

npm update

npm will check all packages for a newer version that satisfies your versioning constraints.

You can specify a single package to update as well:

Versioning

In addition to plain downloads, npm also manages versioning, so you can specify any specific version of a package, or require a version higher or lower than what you need.

Many times you'll find that a library is only compatible with a major release of another library.

Or a bug in the latest release of a lib, still unfixed, is causing an issue.

Specifying an explicit version of a library also helps to keep everyone on the same exact version of a package, so that the whole team runs the same version until the package.json file is updated.

In all those cases, versioning helps a lot, and npm follows the semantic versioning (semver) standard.

You can install a specific version of a package, by running

npm install <package-name>@<version>

Running Tasks

The package.json file supports a format for specifying command line tasks that can be run by using

npm run <task-name>

For example:

{ "scripts": { "start-dev": "node lib/server-development", "start": "node lib/server-production" }}

It's very common to use this feature to run Webpack:

{ "scripts": { "watch": "webpack --watch --progress --colors --config webpack.conf.js", "dev": "webpack --progress --colors --config webpack.conf.js", "prod": "NODE_ENV=production webpack -p --config webpack.conf.js" }}

So instead of typing those long commands, which are easy to forget or mistype, you can run

$ npm run watch$ npm run dev$ npm run prod

Shell Session

Copy to clipboard
Node.js — An introduction to the npm package manager (2024)
Top Articles
Financial coach vs financial adviser: what's the difference?
How Tunnels Work
Fiskars X27 Kloofbijl - 92 cm | bol
Jail Inquiry | Polk County Sheriff's Office
Craigslist Free En Dallas Tx
Manhattan Prep Lsat Forum
Algebra Calculator Mathway
What Are the Best Cal State Schools? | BestColleges
T Mobile Rival Crossword Clue
From Algeria to Uzbekistan-These Are the Top Baby Names Around the World
Sprague Brook Park Camping Reservations
Tyrunt
Items/Tm/Hm cheats for Pokemon FireRed on GBA
Jack Daniels Pop Tarts
Classroom 6x: A Game Changer In The Educational Landscape
7 Low-Carb Foods That Fill You Up - Keto Tips
ocala cars & trucks - by owner - craigslist
Walmart Windshield Wiper Blades
Conan Exiles Thrall Master Build: Best Attributes, Armor, Skills, More
I Touch and Day Spa II
Pricelinerewardsvisa Com Activate
Glenda Mitchell Law Firm: Law Firm Profile
Scout Shop Massapequa
Sea To Dallas Google Flights
Ivegore Machete Mutolation
Anotherdeadfairy
F45 Training O'fallon Il Photos
Sofia the baddie dog
Page 2383 – Christianity Today
Wood Chipper Rental Menards
Radical Red Ability Pill
Hwy 57 Nursery Michie Tn
Rek Funerals
Nurofen 400mg Tabletten (24 stuks) | De Online Drogist
Vlacs Maestro Login
Account Now Login In
Fox And Friends Mega Morning Deals July 2022
Mg Char Grill
Supermarkt Amsterdam - Openingstijden, Folder met alle Aanbiedingen
Devin Mansen Obituary
Naya Padkar Newspaper Today
Imperialism Flocabulary Quiz Answers
How to Draw a Sailboat: 7 Steps (with Pictures) - wikiHow
Is Arnold Swansinger Married
Clausen's Car Wash
US-amerikanisches Fernsehen 2023 in Deutschland schauen
Is Ameriprise A Pyramid Scheme
Ghareeb Nawaz Texas Menu
Yale College Confidential 2027
Where and How to Watch Sound of Freedom | Angel Studios
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6108

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.