Introduction to NPM | Lecture 2

 

Here are concise notes on NPM (Node Package Manager):


Introduction to NPM

What is NPM?

  • NPM (Node Package Manager) is a package manager for JavaScript.
  • It comes bundled with Node.js.
  • Used to install, share, manage, and update JavaScript libraries and tools.

Core Features of NPM:

  1. Package Installation:
    • Allows developers to install and use pre-built libraries or tools.
    • Packages are hosted on the NPM registry.
  2. Dependency Management:
    • Automatically tracks and manages libraries that a project depends on.
  3. Version Control:
    • Supports installing specific versions of packages and updating them to newer versions if needed.
  4. Custom Scripts:
    • Enables running custom-defined commands using npm run.
  5. Publishing:
    • Developers can publish their own packages to the NPM registry.

Common Commands:

  1. Installing Packages:
    • Locally: Installs in the project directory.

o   npm install <package-name>

    • Globally: Installs for use across projects.

o   npm install -g <package-name>

  1. Uninstalling Packages:

3.  npm uninstall <package-name>

  1. Updating Packages:

5.  npm update <package-name>

  1. Viewing Installed Packages:
    • Local:

o   npm list

    • Global:

o   npm list -g

  1. Initializing a Project:
    • Creates a package.json file for the project.

o   npm init

  1. Running Scripts:
    • Scripts defined in package.json can be run using:

o   npm run <script-name>


Key Files:

  1. package.json:
    • A configuration file that stores metadata about the project, such as:
      • Project name, version, description
      • Dependencies
      • Scripts for tasks
  2. package-lock.json:
    • A file that locks the exact versions of installed dependencies to ensure consistency.

Types of Dependencies:

  1. Regular Dependencies: Installed for runtime use.

2.  npm install <package-name>

  1. Development Dependencies: Installed for development purposes only.

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


Advantages of NPM:

  1. Huge Ecosystem:
    • Over a million open-source packages available.
  2. Ease of Use:
    • Simple commands for package management.
  3. Scalability:
    • Efficient handling of large-scale projects with complex dependencies.

Example Workflow:

  1. Initialize a Project:

2.  npm init

  1. Install a Library (e.g., Express):

4.  npm install express

  1. Use the Installed Library:

6.  const express = require('express');

7.  const app = express();

8.   

9.  app.get('/', (req, res) => {

10.  res.send('Hello, NPM!');

11.});

12. 

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


Let me know if you'd like further clarification or examples!

 

Comments

Popular posts from this blog

Introduction to Node.js | Lecture 1

Modules in Node.js | Lecture 4