Definitive Guide to NPM : The Node.js Package Manager (2024)

NPM is the package manager for Javascript programming language. It is also the default package manager for Node.js.

NPM makes it really easy for developers to share and reuse code. Let’s say that you have written some code to solve a particular problem for your application, and you think that your code can be reused by other developers for solving a similar problem in their applications.

You can use npm to publish your code to npm registry so that other developers can download and use your code in their projects.

Similarly, You can also search and download reusable codes submitted by other developers and use it in your project.

These bits of reusable codes are called modules or packages. A package is nothing but a directory with one or more source files in it, along with a package.json file that contains some meta data about the package.

The package.json file contains several important information about the package including but not limited to the following -

  • name : Name of the package.
  • description : An optional description for the package
  • version : Version information in the form of major.minor.patch, ex - 1.2.0
  • main : The main entry file for the package.
  • author : Authors of this package
  • license : License information.
  • dependencies : A typical application generally depends on several other packages. All the packages that this application depends on is listed here.
  • devDependencies : Some dependencies are only required at the time of development and testing. Those dependencies are specified in this field.

Note that, not all the fields in package.json file are mandatory, but It must have at least the name and the version fields.

Installing NPM

NPM comes bundled with node.js, so you don’t need to install it separately. When we installed node.js in the previous tutorial, npm was also installed in the system.

You can verify npm installation by typing the following in your terminal -

$ npm --version3.10.9

Initializing a Node.js Project using NPM

You can use npm init command to initialize a project with a package.json file.

Let’s see it in action! Fire up your terminal and create a new folder called hello-npm.

$ mkdir hello-npm

Now cd to hello-npm directory and type npm init. When you type npm init in your terminal, an interactive prompt will appear which will walk you through creating a package.json file.

NPM will try to guess the default values for all the fields. If you’re happy with the default values, just press enter and move forward, otherwise type the desired value for that field and press enter.

Once all the values are entered, It will ask for confirmation. Type yes, if everything looks OK to you -

$ cd hello-npm$ npm initThis utility will walk you through creating a package.json file.It only covers the most common items, and tries to guess sensible defaults.See `npm help json` for definitive documentation on these fieldsand exactly what they do.Use `npm install <pkg> --save` afterwards to install a package andsave it as a dependency in the package.json file.Press ^C at any time to quit.name: (hello-npm) version: (1.0.0) description: My first npm based applicationentry point: (index.js) server.jstest command: git repository: keywords: NPM Tutorialauthor: callicoderlicense: (ISC) MITAbout to write to /Users/rajeevkumarsingh/hello-npm/package.json:{ "name": "hello-npm", "version": "1.0.0", "description": "My first NPM based application", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "NPM", "Tutorial" ], "author": "callicoder", "license": "MIT"}Is this ok? (yes) yes

Installing packages locally

You can install a package locally using npm install command -

npm install my-package

The npm install command will create a node_modules folder in the current directory (if it doesn’t exist already), and download the package to that directory.

If you want to install the package and also add it to the dependency list in the package.json file, then you can use --save option -

npm install my-package --save

Let’s install a package named express to our hello-npm application. Express is one of the most popular web frameworks for node.js -

npm install express --save

If you check the package.json file after running the above command, express will be listed in the dependencies field -

{ "name": "hello-npm", "version": "1.0.0", "description": "My first NPM based application", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "NPM", "Tutorial" ], "author": "callicoder", "license": "MIT", "dependencies": { "express": "^4.15.3" }}

NPM, by default, installs the latest version of the package available in the npm registry. If you want to install a specific version, you can append the version number after an @ sign with the package name -

npm install [email protected] --save

Installing packages globally

By default, npm installs packages locally inside the project’s root folder. If you want to install a package globally, you can use -g or --global option.

These packages are installed in the system directory and are accessed from the command line. They can not be imported using require() statement.

In the following example, We install grunt-cli, a command line utility for grunt build system -

npm install grunt-cli -g

Once installed, you can access grunt from the command line -

$ grunt

Updating a package

You can use npm update command to update a package. It downloads and installs the latest version of the package and also modifies the package.json file with the new version number -

$ npm update express

Uninstalling a package

Uninstalling a package is really easy with npm. Just type npm uninstall package_name and you are done.

$ npm uninstall express

Searching for Packages

You can use npm search command to search for packages in the npm registry. Let’s see if there is a node.js package for working with aws -

$ npm search aws

This command searches for all the packages matching the keyword aws and gives the output in the following format -

Publishing a package to the npm registry

So, you have developed your application and it’s time to publish it to npm registry so that other developers can reuse your code and get benefited.

You can use npm publish command to publish your package to the npm registry. But first you need to register yourself with npm. You can do that by using npm adduser command.

$ npm adduserUsername: callicoderPassword: Email: (this IS public) [email protected] in as callicoder on https://registry.npmjs.org/.

If you’re already registered with npm, you can use npm login command to login. After logging in, type the following command to publish your package -

$ npm publish

There is one caveat here. The name of your package must be unique, otherwise publish will fail. Once published, you can view your package by going to https://npmjs.com/package/<package_name>.

Conclusion

We covered quite a lot in this tutorial. We learned the basics of npm. We learned how to initialize, search, install, uninstall and publish node.js packages.

Thank you for reading. Please ask any questions in the comment section below.

Definitive Guide to NPM : The Node.js Package Manager (2024)
Top Articles
17 Premium Economy Seats Worth the Upgrade
Ram TRX Sandblast Production Numbers Revealed by Dealer Document
Automated refuse, recycling for most residences; schedule announced | Lehigh Valley Press
7 C's of Communication | The Effective Communication Checklist
Design215 Word Pattern Finder
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Pinellas County Jail Mugshots 2023
Mackenzie Rosman Leaked
Arkansas Gazette Sudoku
Beacon Schnider
877-668-5260 | 18776685260 - Robocaller Warning!
Self-guided tour (for students) – Teaching & Learning Support
Xrarse
Directions To Lubbock
About Goodwill – Goodwill NY/NJ
Scentsy Dashboard Log In
How Quickly Do I Lose My Bike Fitness?
ATV Blue Book - Values & Used Prices
General Info for Parents
Breakroom Bw
Divina Rapsing
Earl David Worden Military Service
Happy Life 365, Kelly Weekers | 9789021569444 | Boeken | bol
Jordan Poyer Wiki
Obituaries Milwaukee Journal Sentinel
Table To Formula Calculator
Restored Republic
Boneyard Barbers
Solarmovie Ma
Pnc Bank Routing Number Cincinnati
Everything You Need to Know About NLE Choppa
آدرس جدید بند موویز
The 50 Best Albums of 2023
Die Filmstarts-Kritik zu The Boogeyman
The best Verizon phones for 2024
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
Ticket To Paradise Showtimes Near Regal Citrus Park
Final Jeopardy July 25 2023
301 Priest Dr, KILLEEN, TX 76541 - HAR.com
Tyler Perry Marriage Counselor Play 123Movies
Wal-Mart 140 Supercenter Products
Cnp Tx Venmo
Setx Sports
Chathuram Movie Download
Tgirls Philly
Rush Copley Swim Lessons
Brother Bear Tattoo Ideas
Searsport Maine Tide Chart
Joblink Maine
Sherwin Source Intranet
Who uses the Fandom Wiki anymore?
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6398

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.