npm Introduction 🎯

beginner
14 min

npm Introduction 🎯

Welcome to our Node.js tutorial series! Today, we're diving into npm (Node Package Manager), an essential tool for every Node.js developer. Let's get started!

What is npm? 📝

npm is a package manager for Node.js. It helps us to manage and share open-source packages easily. With npm, you can download and install thousands of reusable packages for your projects, saving you time and effort.

Installing npm 💡

Before you can use npm, you need to install it on your system. If you already have Node.js installed, npm comes bundled with it. To check if npm is installed, run this command in your terminal or command prompt:

bash
npm -v

If you don't have Node.js installed, download it from the official website.

Using npm ✅

Let's explore how to use npm in your projects. We'll create a simple Node.js application and install a package called express, a popular web framework.

Setting up a new project 📝

Create a new folder for your project and navigate into it:

bash
mkdir my-project && cd my-project

Initializing a new npm project 💡

Initialize your project with npm by running the following command:

bash
npm init

You'll be asked to enter various details about your project. You can accept the defaults by pressing Enter for each question.

Installing express 💡

Now, let's install express using npm:

bash
npm install express

Once installed, you can require it in your code:

javascript
const express = require('express'); const app = express(); // Your code here

Exploring npm packages 💡

You can search for packages on the npm registry using the following command:

bash
npm search <package-name>

To install a specific version of a package, use:

bash
npm install <package-name>@<version>

Understanding npm scripts 💡

npm scripts allow you to define custom commands for your projects. To create a script, add it to the scripts section in your project's package.json file:

json
{ "scripts": { "start": "node app.js" } }

Now, you can run this script with the following command:

bash
npm run start

Managing project dependencies 📝

npm keeps track of your project's dependencies in the package.json file. When you share your project with others, they can easily install the required dependencies using npm.

Quiz 🎯

Quick Quiz
Question 1 of 1

What does npm stand for?

Quick Quiz
Question 1 of 1

How do you install npm?

That's it for our npm introduction! In the next lesson, we'll dive deeper into npm and learn how to create, publish, and manage our own packages. Stay tuned! 🎉