Node.js - NPM

Node Package Manager (NPM) iki ana işlev sağlar -

  • Aranabilecektir node.js paketler / modülleri için Çevrimiçi depoları search.nodejs.org

  • Node.js paketlerini yüklemek, Node.js paketlerinin sürüm yönetimi ve bağımlılık yönetimini yapmak için komut satırı yardımcı programı.

NPM, v0.6.3 sürümünden sonra Node.js yüklenebilirleriyle birlikte gelir. Aynısını doğrulamak için konsolu açın ve aşağıdaki komutu yazın ve sonucu görün -

$ npm --version
2.7.1

NPM'nin eski bir sürümünü çalıştırıyorsanız, onu en son sürüme güncellemek oldukça kolaydır. Kökten şu komutu kullanın -

$ sudo npm install npm -g
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
[email protected] /usr/lib/node_modules/npm

NPM kullanarak Modüllerin Kurulması

Herhangi bir Node.js modülünü yüklemek için basit bir sözdizimi vardır -

$ npm install <Module Name>

Örneğin, aşağıdaki ifade adlı ünlü bir Node.js web çerçeve modülünü kurma komutu verilmiştir -

$ npm install express

Şimdi bu modülü js dosyanızda aşağıdaki gibi kullanabilirsiniz -

var express = require('express');

Global vs Yerel Kurulum

Varsayılan olarak, NPM herhangi bir bağımlılığı yerel modda yükler. Burada yerel mod, Node uygulamasının bulunduğu klasörde bulunan node_modules dizinindeki paket kurulumunu ifade eder. Yerel olarak dağıtılan paketlere, require () yöntemi aracılığıyla erişilebilir. Örneğin, ekspres modülü kurduğumuzda, ekspres modülü kurduğu mevcut dizinde node_modules dizini oluşturdu.

$ ls -l
total 0
drwxr-xr-x 3 root root 20 Mar 17 02:23 node_modules

Alternatif olarak kullanabilirsiniz npm ls yerel olarak kurulu tüm modülleri listelemek için komut.

Global olarak kurulan paketler / bağımlılıklar sistem dizininde saklanır. Bu tür bağımlılıklar, herhangi bir node.js'nin CLI (Komut Satırı Arabirimi) işlevinde kullanılabilir, ancak doğrudan Node uygulamasında require () kullanılarak içe aktarılamaz. Şimdi global kurulumu kullanarak ekspres modülü kurmayı deneyelim.

$ npm install express -g

Bu benzer bir sonuç üretecek ancak modül global olarak yüklenecek. Buradaki ilk satır, modül sürümünü ve kurulduğu yeri gösterir.

[email protected] /usr/lib/node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected])

Global olarak kurulu tüm modülleri kontrol etmek için aşağıdaki komutu kullanabilirsiniz -

$ npm ls -g

Package.json kullanma

package.json, herhangi bir Node uygulamasının / modülünün kök dizininde bulunur ve bir paketin özelliklerini tanımlamak için kullanılır. Mevcut ekspres paketin package.json dosyasını açalımnode_modules/express/

{
   "name": "express",
      "description": "Fast, unopinionated, minimalist web framework",
      "version": "4.11.2",
      "author": {
      
         "name": "TJ Holowaychuk",
         "email": "[email protected]"
      },
   
   "contributors": [{
      "name": "Aaron Heckmann",
      "email": "[email protected]"
   }, 
   
   {
      "name": "Ciaran Jessup",
      "email": "[email protected]"
   },
   
   {
      "name": "Douglas Christopher Wilson",
      "email": "[email protected]"
   },
   
   {
      "name": "Guillermo Rauch",
      "email": "[email protected]"
   },
   
   {
      "name": "Jonathan Ong",
      "email": "[email protected]"
   },
   
   {
      "name": "Roman Shtylman",
      "email": "[email protected]"
   },
   
   {
      "name": "Young Jae Sim",
      "email": "[email protected]"
   } ],
   
   "license": "MIT", "repository": {
      "type": "git",
      "url": "https://github.com/strongloop/express"
   },
   
   "homepage": "https://expressjs.com/", "keywords": [
      "express",
      "framework",
      "sinatra",
      "web",
      "rest",
      "restful",
      "router",
      "app",
      "api"
   ],
   
   "dependencies": {
      "accepts": "~1.2.3",
      "content-disposition": "0.5.0",
      "cookie-signature": "1.0.5",
      "debug": "~2.1.1",
      "depd": "~1.0.0",
      "escape-html": "1.0.1",
      "etag": "~1.5.1",
      "finalhandler": "0.3.3",
      "fresh": "0.2.4",
      "media-typer": "0.3.0",
      "methods": "~1.1.1",
      "on-finished": "~2.2.0",
      "parseurl": "~1.3.0",
      "path-to-regexp": "0.1.3",
      "proxy-addr": "~1.0.6",
      "qs": "2.3.3",
      "range-parser": "~1.0.2",
      "send": "0.11.1",
      "serve-static": "~1.8.1",
      "type-is": "~1.5.6",
      "vary": "~1.0.0",
      "cookie": "0.1.2",
      "merge-descriptors": "0.0.2",
      "utils-merge": "1.0.0"
   },
   
   "devDependencies": {
      "after": "0.8.1",
      "ejs": "2.1.4",
      "istanbul": "0.3.5",
      "marked": "0.3.3",
      "mocha": "~2.1.0",
      "should": "~4.6.2",
      "supertest": "~0.15.0",
      "hjs": "~0.0.6",
      "body-parser": "~1.11.0",
      "connect-redis": "~2.2.0",
      "cookie-parser": "~1.3.3",
      "express-session": "~1.10.2",
      "jade": "~1.9.1",
      "method-override": "~2.3.1",
      "morgan": "~1.5.1",
      "multiparty": "~4.1.1",
      "vhost": "~3.0.0"
   },
   
   "engines": {
      "node": ">= 0.10.0"
   },
   
   "files": [
      "LICENSE",
      "History.md",
      "Readme.md",
      "index.js",
      "lib/"
   ],
   
   "scripts": {
      "test": "mocha --require test/support/env 
         --reporter spec --bail --check-leaks test/ test/acceptance/",
      "test-cov": "istanbul cover node_modules/mocha/bin/_mocha 
         -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",
      "test-tap": "mocha --require test/support/env 
         --reporter tap --check-leaks test/ test/acceptance/",
      "test-travis": "istanbul cover node_modules/mocha/bin/_mocha 
         --report lcovonly -- --require test/support/env 
         --reporter spec --check-leaks test/ test/acceptance/"
   },
   
   "gitHead": "63ab25579bda70b4927a179b580a9c580b6c7ada",
   "bugs": {
      "url": "https://github.com/strongloop/express/issues"
   },
   
   "_id": "[email protected]",
   "_shasum": "8df3d5a9ac848585f00a0777601823faecd3b148",
   "_from": "express@*",
   "_npmVersion": "1.4.28",
   "_npmUser": {
      "name": "dougwilson",
      "email": "[email protected]"
   },
   
   "maintainers": [{
      "name": "tjholowaychuk",
      "email": "[email protected]"
   },
   
   {
      "name": "jongleberry",
      "email": "[email protected]"
   },
   
   {
      "name": "shtylman",
      "email": "[email protected]"
   },
   
   {
      "name": "dougwilson",
      "email": "[email protected]"
   },
   
   {
      "name": "aredridel",
      "email": "[email protected]"
   },
   
   {
      "name": "strongloop",
      "email": "[email protected]"
   },
   
   {
      "name": "rfeng",
      "email": "[email protected]"
   }],
   
   "dist": {
      "shasum": "8df3d5a9ac848585f00a0777601823faecd3b148",
      "tarball": "https://registry.npmjs.org/express/-/express-4.11.2.tgz"
   },
   
   "directories": {},
      "_resolved": "https://registry.npmjs.org/express/-/express-4.11.2.tgz",
      "readme": "ERROR: No README data found!"
}

Package.json Öznitelikleri

  • name - paketin adı

  • version - paketin versiyonu

  • description - paketin açıklaması

  • homepage - paketin ana sayfası

  • author - paketin yazarı

  • contributors - pakete katkıda bulunanların adı

  • dependencies- bağımlılıkların listesi. NPM, paketin node_module klasöründe burada bahsedilen tüm bağımlılıkları otomatik olarak yükler.

  • repository - paketin depo türü ve URL'si

  • main - paketin giriş noktası

  • keywords - anahtar kelimeler

Bir Modülü Kaldırma

Bir Node.js modülünü kaldırmak için aşağıdaki komutu kullanın.

$ npm uninstall express

NPM paketi kaldırdığında, / node_modules / dizininin içeriğine bakarak veya aşağıdaki komutu yazarak doğrulayabilirsiniz:

$ npm ls

Bir Modülü Güncelleme

Package.json'u güncelleyin ve güncellenecek bağımlılığın sürümünü değiştirin ve aşağıdaki komutu çalıştırın.

$ npm update express

Modül Ara

NPM kullanarak bir paket adı arayın.

$ npm search express

Modül Oluşturun

Bir modül oluşturmak için package.json'un üretilmesi gerekir. NPM kullanarak package.json oluşturalım, bu da package.json'un temel iskeletini oluşturacaktır.

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.

See 'npm help json' for definitive documentation on these fields
and exactly what they do.

Use 'npm install <pkg> --save' afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (webmaster)

Modülünüz hakkında gerekli tüm bilgileri sağlamanız gerekecektir. Talep edilen çeşitli bilgilerin anlamlarını anlamak için yukarıda belirtilen package.json dosyasından yardım alabilirsiniz. Package.json oluşturulduktan sonra, geçerli bir e-posta adresi kullanarak kendinizi NPM havuz sitesine kaydetmek için aşağıdaki komutu kullanın.

$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) [email protected]

Şimdi modülünüzü yayınlama zamanı -

$ npm publish

Modülünüzle ilgili her şey yolundaysa, o zaman havuzda yayınlanacak ve diğer Node.js modülleri gibi NPM kullanılarak yüklenmek için erişilebilir olacaktır.