Nodejs-npm

提供:Dev Guides
移動先:案内検索

Node.js-NPM

Node Package Manager(NPM)は2つの主要な機能を提供します-

  • https://search.nodejs.org [search.nodejs.org]で検索可能なnode.jsパッケージ/モジュールのオンラインリポジトリ
  • Node.jsパッケージをインストールし、Node.jsパッケージのバージョン管理と依存関係管理を行うコマンドラインユーティリティ。

NPMは、v0.6.3バージョン以降のNode.jsインストール可能ファイルにバンドルされています。 同じことを確認するには、コンソールを開き、次のコマンドを入力して結果を確認します-

$ npm --version
2.7.1

NPMの古いバージョンを実行している場合、それを最新バージョンに更新するのは非常に簡単です。 ルートから次のコマンドを使用するだけです-

$ 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を使用したモジュールのインストール

Node.jsモジュールをインストールする簡単な構文があります-

$ npm install <Module Name>

たとえば、エクスプレスと呼ばれる有名なNode.js Webフレームワークモジュールをインストールするコマンドは次のとおりです-

$ npm install express

これで、次のようにjsファイルでこのモジュールを使用できます-

var express = require('express');

グローバルインストールとローカルインストール

デフォルトでは、NPMはローカルモードで依存関係をインストールします。 ここで、ローカルモードとは、Nodeアプリケーションが存在するフォルダーにあるnode_modulesディレクトリへのパッケージのインストールを指します。 ローカルに展開されたパッケージには、require()メソッドを介してアクセスできます。 たとえば、エクスプレスモジュールをインストールすると、エクスプレスモジュールをインストールした現在のディレクトリにnode_modulesディレクトリが作成されます。

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

または、 npm ls コマンドを使用して、ローカルにインストールされているすべてのモジュールをリストダウンできます。

グローバルにインストールされたパッケージ/依存関係は、システムディレクトリに保存されます。 このような依存関係は、node.jsのCLI(コマンドラインインターフェイス)関数で使用できますが、Nodeアプリケーションでrequire()を使用して直接インポートすることはできません。 次に、グローバルインストールを使用して、エクスプレスモジュールをインストールしてみましょう。

$ npm install express -g

これにより同様の結果が得られますが、モジュールはグローバルにインストールされます。 ここで、最初の行はモジュールのバージョンとインストールされる場所を示しています。

[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])

次のコマンドを使用して、グローバルにインストールされているすべてのモジュールを確認できます-

$ npm ls -g

package.jsonを使用する

package.jsonは、任意のNodeアプリケーション/モジュールのルートディレクトリに存在し、パッケージのプロパティを定義するために使用されます。 node_modules/express/ にあるエクスプレスパッケージのpackage.jsonを開きましょう

{
   "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の属性

  • name -パッケージの名前
  • version -パッケージのバージョン
  • description -パッケージの説明
  • homepage -パッケージのホームページ
  • author -パッケージの作成者
  • contributors -パッケージへの貢献者の名前
  • dependencies -依存関係のリスト。 NPMは、ここに記載されているすべての依存関係をパッケージのnode_moduleフォルダーに自動的にインストールします。
  • repository -パッケージのリポジトリタイプとURL
  • main -パッケージのエントリポイント
  • キーワード-キーワード

モジュールのアンインストール

Node.jsモジュールをアンインストールするには、次のコマンドを使用します。

$ npm uninstall express

NPMがパッケージをアンインストールしたら、/node_modules/ディレクトリの内容を見て確認するか、次のコマンドを入力します-

$ npm ls

モジュールの更新

package.jsonを更新し、更新する依存関係のバージョンを変更して、次のコマンドを実行します。

$ npm update express

モジュールを検索する

NPMを使用してパッケージ名を検索します。

$ npm search express

モジュールを作成する

モジュールを作成するには、package.jsonを生成する必要があります。 NPMを使用してpackage.jsonを生成します。これにより、package.jsonの基本的なスケルトンが生成されます。

$ 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)

モジュールに関するすべての必要な情報を提供する必要があります。 上記のpackage.jsonファイルのヘルプを利用して、必要なさまざまな情報の意味を理解できます。 package.jsonが生成されたら、次のコマンドを使用して、有効な電子メールアドレスを使用してNPMリポジトリサイトに自分自身を登録します。

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

あなたのモジュールを公開する時が来ました-

$ npm publish

モジュールで問題がなければ、リポジトリに公開され、他のNode.jsモジュールと同様にNPMを使用してインストールできるようになります。