100===Dev Ops/NPM

NPM Introduced

블로글러 2024. 5. 26. 11:04

In this lesson, we'll explore npm (Node Package Manager), how it works, and its significance in managing JavaScript packages and dependencies.

The Big Picture

Imagine you're building a house. You need various materials like bricks, cement, wood, etc. Instead of creating these materials from scratch, you get them from suppliers. In the world of JavaScript, npm is like your supplier, providing you with the necessary packages (materials) to build your project (house) efficiently.

Core Concepts

  1. npm: Node Package Manager, a tool for managing JavaScript packages.
  2. Packages: Reusable code modules that can be shared and used in different projects.
  3. Dependencies: Packages that your project needs to function correctly.
  4. Registry: A large database where npm packages are stored and can be downloaded from.

Detailed Walkthrough

Let's break down how npm works and how you can use it:

  1. Installing npm: npm comes with Node.js. To check if you have npm installed, you can use the command:

    npm -v

    This will show the version of npm installed.

  2. Initializing a Project: To start a new project, you create a package.json file using:

    npm init

    This file contains metadata about your project and lists its dependencies.

  3. Installing Packages: To install a package, use:

    npm install package-name

    This downloads the package and adds it to the node_modules directory.

  4. Saving Dependencies: By default, installed packages are added to the dependencies section in package.json. For example:

    "dependencies": {
      "express": "^4.17.1"
    }

    This indicates that the project depends on the Express framework.

  5. DevDependencies: These are packages needed only for development (e.g., testing tools). Install them with:

    npm install package-name --save-dev
  6. Scripts: You can define scripts in package.json to automate tasks. For example:

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

    Run these scripts using:

    npm run start

Understanding Through an Example

Let's say you're building a simple web server using Express. Here's how you'd do it with npm:

  1. Initialize your project:

    npm init -y

    This creates a default package.json file.

  2. Install Express:

    npm install express

    This adds Express to your project and updates package.json and node_modules.

  3. Create a basic server:

    // app.js
    const express = require('express');
    const app = express();
    const port = 3000;
    
    app.get('/', (req, res) => {
      res.send('Hello World!');
    });
    
    app.listen(port, () => {
      console.log(`Server is running at http://localhost:${port}`);
    });
  4. Define a start script in package.json:

    "scripts": {
      "start": "node app.js"
    }
  5. Run the server:

    npm run start

Conclusion and Summary

npm simplifies the management of JavaScript dependencies by providing an easy way to install, update, and share packages. It automates tasks and ensures that your project has all the necessary tools to function correctly.

Test Your Understanding

  1. What command is used to initialize a new npm project?
  2. How do you install a package and save it as a development dependency?
  3. What is the purpose of the node_modules directory?
  4. How can you define and run custom scripts in package.json?

Reference

For more detailed information on npm, you can visit the official npm documentation.

728x90