Progressive Full Stack Application Development with Live Projects

JavaScript Lesson

NPM for JavaScript Programmers

What is a Package Manager?

A package manager is a tool that helps manage dependencies (libraries or frameworks) in your project. It automates the process of installing, upgrading, configuring, and removing packages or libraries that your project needs to function.

For JavaScript, the most common package managers are:

  • npm (Node Package Manager): Comes with Node.js and is the most widely used package manager for JavaScript.
  • Yarn: Another popular package manager that also works well with JavaScript.

Why Do We Need Package Managers Like npm?

1. Dependency Management

Modern JavaScript projects rely on multiple libraries and frameworks (e.g., React, Lodash, Express). Managing them manually would be cumbersome and error-prone. npm helps you easily install and update these dependencies..

2. Version Control

Packages have specific versions. npm allows you to manage which version of a library you’re using to avoid breaking changes or incompatibility issues.

3. Easy Install and Distribution

Instead of manually downloading libraries, npm helps you download and manage these libraries from the npm registry.

4. Automating Repetitive Tasks

npm can also automate tasks like running tests, building code, or deploying your project.

How to Use npm with JavaScript Projects?

1. Install Node.js & npm

To use npm, you first need to install Node.js, which comes with npm built-in.

  • Go to Node.js website and download the LTS version (Recommended for most users).

  • After installation, you can check if npm is installed by running the following commands in your terminal:

				
					node -v
npm -v

				
			

If you see version numbers printed for both node and npm, you’re good to go!

2. Create a New Project

In a new or existing JavaScript project, you can start by initializing npm.

  • Navigate to your project directory

				
					cd your-project-directory

				
			
  • Initialize a new npm project by running
				
					npm init

				
			

This command will ask you for some details (like project name, version, entry point, etc.) and generate a package.json file, which contains metadata about your project and the dependencies it uses. If you want to skip the prompts, you can run.

				
					 ```bash
 npm init -y
 ```

				
			

This will automatically generate a default package.json file.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

3. Install Dependencies

Once your project is initialized, you can install packages (dependencies) using npm.

For example, to install React:

				
					npm install react

				
			

This will install the react package and add it to the node_modules folder and package.json file under dependencies.

To install a devDependency (something only needed for development, like a testing library or build tool):

				
					npm install --save-dev jest

				
			

4. Using Installed Packages

After installing a package, you can use it in your JavaScript files. For example:

				
					const React = require('react');

				
			

Or using ES6 syntax:

				
					import React from 'react';

				
			

5. Running Scripts with npm

In your package.json file, you can define scripts that run specific commands.

For example, you can define a script to start a server or run tests

				
					"scripts": {
  "start": "node server.js",
  "test": "jest"
}

				
			

Then, you can run these scripts from the command line like so:

				
					npm run start
npm run test

				
			

6. Updating and Removing Packages

To update an installed package to the latest version, use:

				
					npm update package-name

				
			

To remove a package

				
					npm uninstall package-name

				
			

7. Using Global Packages

You can also install packages globally on your system. This is useful for command-line tools (e.g., Angular CLI, TypeScript compiler).

For example, to install a package globally:

				
					npm install -g typescript

				
			

This would allow you to use TypeScript from the command line, no matter where you are in your system.

Example Workflow

  1. Start a New Project
				
					mkdir my-project
cd my-project
npm init -y

				
			

2. Install Dependencies

				
					npm install express

				
			

3. Use Dependencies in your JavaScript code (e.g., index.js):

				
					const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

				
			

4. Run Your Application

				
					node index.js

				
			

5. Manage Dependencies as needed by running npm install or npm update

Summary of Common npm Commands

  • npm init: Initializes a new project (creates package.json).
  • npm install <package>: Installs a package.
  • npm install --save-dev <package>: Installs a development dependency.
  • npm uninstall <package>: Removes a package.
  • npm update: Updates all packages.
  • npm run <script>: Runs a script defined in the package.json.
  • npm list: Shows the installed packages and their versions.

By using npm, you’re simplifying the process of managing libraries and tools that your JavaScript project needs to function. It helps you focus more on writing your code rather than managing dependencies manually.