Mastering npm: A Beginner’s Guide to Node Package Manager

Mastering npm: A Beginner’s Guide to Node Package Manager

npm is an essential tool for JavaScript developers, allowing them to manage and install packages for their projects effortlessly.

What Is npm?

The Node Package Manager (npm) is one of the largest software registries globally, bundled with Node.js, an open-source server environment. Whether you’re a beginner or an intermediate user, understanding npm will enhance your development workflow significantly.

How Does npm Work?

  • Repository – npm serves as a repository for publishing open-source Node projects, where developers can share their source code with others. Popular packages like Angular, React, and jQuery can be downloaded from the npm repository.
  • Command-line Interface (CLI) – Developers use the npm CLI from a terminal to install or uninstall packages and manage versions or dependencies.
  • Registry – Any package or module you install using the npm CLI is downloaded from the npm public registry, the JavaScript software database.

What Is package.json?

Every npm project contains a package.json file located in the root directory. It contains metadata for npm projects or packages, such as package versions and contributors. The package.json file simplifies the process of identifying, managing, and installing a package. That’s why it’s essential to include package.json before publishing projects on the npm registry.

{ "name": "<a href="https://hostinger.com?REFERRALCODE=1CRYPTO99" rel="sponsored noopener" target="_blank">Hostinger</a>-npm", "version": "1.0.0", "description": "npm guide for beginner", "main": "beginner-npm.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "dependencies": { "express": "^4.17.1" }, "repository": { "type": "git", "url": "https://github.com/hostinger/example.git" }, "keywords": [ "npm", "example", "basic" ], "author": "Hostinger International", "license": "MIT" }

To help you understand package.json, we’ll go over the most important lines from the previous example:

"name": "<a href="https://hostinger.com?REFERRALCODE=1CRYPTO99" rel="sponsored noopener" target="_blank">Hostinger</a>-npm", – The package name should be less than 214 characters and consist of lowercase letters only. If you want to publish a tool on the npm registry, its name should differ from any existing packages.

"version": "1.0.0", – The version specifies the current version of your project, following semantic versioning rules.

"description": "npm guide for beginner", – Include the package information in the description to help users discover your project on the npm registry.

For more information on the other fields of package.json, refer to the official documentation.

What Are Dependencies and devDependencies?

Dependencies and devDependencies are fields in package.json that list all packages a project depends on. The dependency field includes all third-party packages required for your project to work. On the other hand, the devDependency property contains packages that are only necessary during development.

How to Install npm Modules and Start Projects?

Before using npm, make sure that Node.js is installed. To do so, open your terminal and run this command:

node -v

The terminal window should display the Node.js version installed on your system:

V14.17.5

Do the same for npm by using the following command:

npm -v

It should also return a version number like this:

6.14.14

If Node.js hasn’t been installed yet, download it from the official website.

In the following sections, we’ll discuss some basic npm commands.

Initializing a Project with npm

To start a new project, run the following command from the npm CLI:

npm init

This command will create a project’s package.json file in the current directory. After running the npm init command, the terminal will display a series of prompts for you to input your project information.

Here’s an example of how we fill the package information:

name: (test-npm)<a href="https://hostinger.com?REFERRALCODE=1CRYPTO99" rel="sponsored noopener" target="_blank">Hostinger</a>-npm version: (1.0.0)1.0.0 description: npm guide for beginner entry point: (index.js) beginner-npm.js test command: git repository: keywords: npm, example, beginner author: Hostinger International license: (ISC) MIT

If you want to generate the package.json file with default values without responding to the npm init prompts, use this command:

npm init --yes

Installing npm Modules in Your Project

It’s essential to learn how to install a package when getting started with npm. To add a package to your project, run either of these commands:

npm install <package-name> // OR npm i <package-name>

For example, to install Express, a popular web framework, use the following command:

npm install express

Installing a Package as a Development Dependency

To install tools that will only be used in the development environment, use the following command:

npm install <package-name> --save-dev

For example, to install Nodemon, type this into the terminal:

npm install nodemon --save-dev

Installing a Module Globally on Your System

To install an npm package to be used from the command line, enter either of the following commands:

npm install <package-name> --global // OR npm i <package-name> -g

Updating npm

Regularly update local and global packages downloaded from the npm registry to keep your code secure and stable. To update all the packages in your project, run this command:

npm update

To update a single module, input this:

npm update <package-name>

To update your Node package manager itself, run this command:

npm install npm@latest -g

Boost Your Development with Hostinger

Looking to enhance your development environment? Consider using a VPS from Hostinger with Node.js pre-installed to streamline your process.

Conclusion

npm is a powerful package manager that allows JavaScript developers to share and deploy useful packages efficiently. With the knowledge of npm, you can easily manage your projects’ dependencies and enhance your development workflow. We hope this guide has provided you with a solid foundation to start working with npm. If you have any questions, feel free to leave a comment below.

Amanda B.
Amanda is a WordPress and digital marketing enthusiast with a passion for helping others grow their businesses and careers. She enjoys producing content that is informative and helpful. When she’s not working, Amanda loves hiking, reading books, and filling her bullet journal.

Download Complete HTML Cheat Sheet

👉 Start your website with Hostinger – get fast, secure hosting here 👈


🔗 Read more from MinimaDesk:


🎁 Download free premium WordPress tools from our Starter Tools page.

Selling SEO Services: A Complete Guide for Beginners
Mastering the Docker Exec Command: Your Ultimate Guide
My Cart
Wishlist
Recently Viewed
Categories