NPM

NPM (Node Package Manager) 의 μ£Όμš” κΈ°λŠ₯

  1. NPMSearch μ—μ„œ 탐색 κ°€λŠ₯ν•œ Node.js νŒ¨ν‚€μ§€/λͺ¨λ“ˆ μ €μž₯μ†Œ

  2. Node.js νŒ¨ν‚€μ§€ μ„€μΉ˜ 및 버전 / ν˜Έν™˜μ„± 관리λ₯Ό ν•  수 μžˆλŠ” μ»€λ§¨λ“œλΌμΈ μœ ν‹Έλ¦¬ν‹°

npm μ„€μΉ˜ 확인

$ npm --version
6.13.4

npm version update

sudo npm install npm -g

Installing Modules using NPM

Syntax to install any Node.js module

npm install <Module Name>

Install a famous Node.js web framework module called express

npm install express

Now you can use this module in your js file as following

var express = require('express');

Global vs Local Installation

κΈ°λ³Έμ μœΌλ‘œλŠ” npm 은 module을 local mode둜 μ„€μΉ˜

  • Local mode

    : packageλ₯Ό λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•œ directory μ•ˆμ— μžˆλŠ” node_modules에 μ„€μΉ˜ν•˜λŠ” 것을 의미

  • Global mode

    : System directory에 μ„€μΉ˜ν•˜λŠ” 것을 의미

expressλ₯Ό κΈ€λ‘œλ²Œ mode둜 μ„€μΉ˜ν•˜κΈ°

$ sudo npm install express -g
/usr/lib
└─┬ express@4.13.4
 β”œβ”€β”¬ accepts@1.2.13
 β”‚ β”œβ”€β”¬ mime-types@2.1.9
 β”‚ β”‚ └── mime-db@1.21.0
 β”‚ └── negotiator@0.5.3
 .... κΈΈμ–΄μ„œ μƒλž΅....
 β”‚ └── statuses@1.2.1
 β”œβ”€β”€ utils-merge@1.0.0
 └── vary@1.0.1
  • ν˜„μž¬ κ²½λ‘œκ°€ μ•„λ‹Œ /usr/lib/node_modules 에 λͺ¨λ“ˆμ„ μ„€μΉ˜ν•˜λŠ” 것 확인 κ°€λŠ₯

  • system에 μ €μž₯ν•˜λ―€λ‘œ, root 계정이 μ•„λ‹ˆλΌλ©΄ μ•žμ— sudoλ₯Ό λΆ™μ—¬μ£Όμ–΄μ•Ό 함!

  • Global mode 둜 μ„€μΉ˜ν•˜μ˜€μ„λ•ŒλŠ”, node applicationμ—μ„œ λ°”λ‘œ require ν•  μˆ˜λŠ” μ—†μŒ

    • but, npm link λͺ…λ Ήμ–΄λ₯Ό μž…λ ₯ν•˜μ—¬ ν•΄λ‹Ή module을 뢈러올 수 있음

    npm install -g express
    cd [local path]/project
    npm link express

package.json

: Node application / module의 κ²½λ‘œμ— μœ„μΉ˜ν•΄ 있으며 pacakge의 속성을 μ •μ˜ν•¨

express둜 projectλ₯Ό μƒμ„±ν–ˆμ„ λ•Œ μƒμ„±λ˜λŠ” package.json

{
  "name": "myapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "jade": "~1.11.0",
    "morgan": "~1.6.1",
    "serve-favicon": "~2.3.0"
  }
}
  • package.json은 projectκ°€ μ˜μ‘΄ν•˜λŠ” moduleκ³Ό module version의 정보λ₯Ό λ‹΄κ³ μžˆμŒ

Uninstalling a Module

npm uninstall express

Updating a Module

npm update express

Search a Module

npm search express
  • 이 λͺ…λ Ήμ–΄λŠ” 처음 μ΄μš©ν•  λ•Œ memory μ—„μ²­ μž‘μ•„λ¨ΉμŒ

    • Cloud IDEλ₯Ό μ‚¬μš©ν•˜κ±°λ‚˜ server에 ram이 1G 정도라면 맀우 μ˜€λž˜κ±Έλ¦¬κ±°λ‚˜ μ—λŸ¬κ°€ 남

    • κ·ΈλŸ΄λ• NPMSearch μ—μ„œ κ²€μƒ‰ν•˜κΈ°!

Last updated