NPM は、NodeJS とともにインストールされるパッケージ管理ツールで、NodeJS コードのデプロイメントにおける多くの問題を解決できます。一般的な使用シナリオは次のとおりです。
新しいバージョンのnodejsにはnpmが統合されているため、npmも以前にインストールされていました。 「npm -v」 と入力して、インストールが成功したかどうかをテストすることもできます。 コマンドは次のとおりです。インストールが成功したことを示すバージョン プロンプトが表示されます。
$ npm -v 2.3.0
古いバージョンの npm をインストールしている場合は、npm コマンドを使用して簡単にアップグレードできます。コマンドは次のとおりです。
$ sudo npm install npm -g /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js npm@2.14.2 /usr/local/lib/node_modules/npm
Windows システムの場合は、次のコマンドを使用します。
npm install npm -g
ミラーコマンド:
npm install -g cnpm --registry=https://registry.npmmirror.com
npm を使用して Node.js モジュールをインストールするための構文は次のとおりです。
$ npm install <Module Name>
次の例では、npm コマンドを使用して、一般的に使用される Node.js Web フレームワーク モジュール express をインストールします。
$ npm install express
インストール後、Express パッケージはプロジェクト ディレクトリの Node_modules ディレクトリに配置されるため、サードパーティ パッケージのパスを指定せずに、コード内で require('express') を渡すだけで済みます。
var Express = require('express');
npmのパッケージインストールはローカルインストール(local)とグローバルインストール(global)の2種類に分かれており、コマンドラインからは例えば-gの有無のみが違います。 <前> npm install Express # ローカルインストール npm install Express -g # グローバルインストール
次のエラーが発生した場合:
npm err! Error: connect ECONNREFUSED 127.0.0.1:8087
解決策は次のとおりです。
$ npm config set proxy null
両方必要な場合は、2 か所にインストールするか、npm リンクを使用する必要があります。
次に、Express をグローバルにインストールします
$ npm install express -g
インストール プロセスでは、次の内容が出力されます。最初の行では、モジュールのバージョン番号とインストール場所が出力されます。
express@4.13.3 node_modules/express ├── escape-html@1.0.2 ├── range-parser@1.0.2 ├── merge-descriptors@1.0.0 ├── array-flatten@1.1.1 ├── cookie@0.1.3 ├── utils-merge@1.0.0 ├── parseurl@1.3.0 ├── cookie-signature@1.0.6 ├── methods@1.1.1 ├── fresh@0.3.0 ├── vary@1.0.1 ├── path-to-regexp@0.1.7 ├── content-type@1.0.1 ├── etag@1.7.0 ├── serve-static@1.10.0 ├── content-disposition@0.5.0 ├── depd@1.0.1 ├── qs@4.0.0 ├── finalhandler@0.4.0 (unpipe@1.0.0) ├── on-finished@2.3.0 (ee-first@1.1.1) ├── proxy-addr@1.0.8 (forwarded@0.1.0, ipaddr.js@1.0.1) ├── debug@2.2.0 (ms@0.7.1) ├── type-is@1.6.8 (media-typer@0.3.0, mime-types@2.1.6) ├── accepts@1.2.12 (negotiator@0.5.3, mime-types@2.1.6) └── send@0.13.0 (destroy@1.0.3, statuses@1.2.1, ms@0.7.1, mime@1.3.4, http-errors@1.3.1)
次のコマンドを使用すると、グローバルにインストールされているすべてのモジュールを表示できます。
$ npm list -g ├─┬ cnpm@4.3.2 │ ├── auto-correct@1.0.0 │ ├── bagpipe@0.3.5 │ ├── colors@1.1.2 │ ├─┬ commander@2.9.0 │ │ └── graceful-readlink@1.0.1 │ ├─┬ cross-spawn@0.2.9 │ │ └── lru-cache@2.7.3 ……
モジュールのバージョン番号を確認したい場合は、次のようにコマンドを使用できます。
$ npm list grunt projectName@projectVersion /path/to/project/folder └── grunt@0.4.1
package.json はモジュールのディレクトリにあり、パッケージのプロパティを定義するために使用されます。 次に、node_modules/express/package.json にある Express パッケージの package.json ファイルを見てみましょう。 内容:
{
"name": "express",
"description": "Fast, unopinionated, minimalist web framework",
"version": "4.13.3",
"author": {
"name": "TJ Holowaychuk",
"email": "tj@vision-media.ca"
},
"contributors": [
{
"name": "Aaron Heckmann",
"email": "aaron.heckmann+github@gmail.com"
},
{
"name": "Ciaran Jessup",
"email": "ciaranj@gmail.com"
},
{
"name": "Douglas Christopher Wilson",
"email": "doug@somethingdoug.com"
},
{
"name": "Guillermo Rauch",
"email": "rauchg@gmail.com"
},
{
"name": "Jonathan Ong",
"email": "me@jongleberry.com"
},
{
"name": "Roman Shtylman",
"email": "shtylman+expressjs@gmail.com"
},
{
"name": "Young Jae Sim",
"email": "hanul@hanul.me"
}
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/strongloop/express.git"
},
"homepage": "http://expressjs.com/",
"keywords": [
"express",
"framework",
"sinatra",
"web",
"rest",
"restful",
"router",
"app",
"api"
],
"dependencies": {
"accepts": "~1.2.12",
"array-flatten": "1.1.1",
"content-disposition": "0.5.0",
"content-type": "~1.0.1",
"cookie": "0.1.3",
"cookie-signature": "1.0.6",
"debug": "~2.2.0",
"depd": "~1.0.1",
"escape-html": "1.0.2",
"etag": "~1.7.0",
"finalhandler": "0.4.0",
"fresh": "0.3.0",
"merge-descriptors": "1.0.0",
"methods": "~1.1.1",
"on-finished": "~2.3.0",
"parseurl": "~1.3.0",
"path-to-regexp": "0.1.7",
"proxy-addr": "~1.0.8",
"qs": "4.0.0",
"range-parser": "~1.0.2",
"send": "0.13.0",
"serve-static": "~1.10.0",
"type-is": "~1.6.6",
"utils-merge": "1.0.0",
"vary": "~1.0.1"
},
"devDependencies": {
"after": "0.8.1",
"ejs": "2.3.3",
"istanbul": "0.3.17",
"marked": "0.3.5",
"mocha": "2.2.5",
"should": "7.0.2",
"supertest": "1.0.1",
"body-parser": "~1.13.3",
"connect-redis": "~2.4.1",
"cookie-parser": "~1.3.5",
"cookie-session": "~1.2.0",
"express-session": "~1.11.3",
"jade": "~1.11.0",
"method-override": "~2.3.5",
"morgan": "~1.6.1",
"multiparty": "~4.1.2",
"vhost": "~3.0.1"
},
"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-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --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/"
},
"gitHead": "ef7ad681b245fba023843ce94f6bcb8e275bbb8e",
"bugs": {
"url": "https://github.com/strongloop/express/issues"
},
"_id": "express@4.13.3",
"_shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
"_from": "express@*",
"_npmVersion": "1.4.28",
"_npmUser": {
"name": "dougwilson",
"email": "doug@somethingdoug.com"
},
"maintainers": [
{
"name": "tjholowaychuk",
"email": "tj@vision-media.ca"
},
{
"name": "jongleberry",
"email": "jonathanrichardong@gmail.com"
},
{
"name": "dougwilson",
"email": "doug@somethingdoug.com"
},
{
"name": "rfeng",
"email": "enjoyjava@gmail.com"
},
{
"name": "aredridel",
"email": "aredridel@dinhe.net"
},
{
"name": "strongloop",
"email": "callback@strongloop.com"
},
{
"name": "defunctzombie",
"email": "shtylman@gmail.com"
}
],
"dist": {
"shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
"tarball": "http://registry.npmjs.org/express/-/express-4.13.3.tgz"
},
"directories": {},
"_resolved": "https://registry.npmjs.org/express/-/express-4.13.3.tgz",
"readme": "ERROR: No README data found!"
}
name - パッケージ名。
version - パッケージのバージョン番号。
description - パッケージの説明。
homepage - パッケージの公式 Web サイトの URL。
author - パッケージの作成者の名前。
contributors - パッケージへの他の貢献者の名前。
dependencies - 依存パッケージのリスト。 依存パッケージがインストールされていない場合、npm は依存パッケージを node_module ディレクトリに自動的にインストールします。
repository - パッケージ コードが保存されるリポジトリのタイプ。git または svn にすることができます。git は Github にある場合もあります。
main - main フィールドはプログラムのメイン エントリ ファイルを指定します。require('moduleName') はこのファイルをロードします。 このフィールドのデフォルト値は、モジュールのルート ディレクトリにあるindex.jsです。
keywords - キーワード
次のコマンドを使用して、Node.js モジュールをアンインストールできます。
$ npm uninstall express
アンインストール後、/node_modules/ ディレクトリに移動してパッケージがまだ存在するかどうかを確認するか、次のコマンドを使用して確認できます。
$ npm ls
次のコマンドを使用してモジュールを更新できます。
$ npm update Express
モジュールを検索するには、次のコマンドを使用します。
$ npm search express
モジュールを作成するには、package.json ファイルが必要です。 NPM を使用して、基本的な結果を含む 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 sensible 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: (node_modules) runoob # 模块名
version: (1.0.0)
description: Node.js 测试模块(www.runoob.com) # 描述
entry point: (index.js)
test command: make test
git repository: https://github.com/runoob/runoob.git # Github 地址
keywords:
author:
license: (ISC)
About to write to ……/node_modules/package.json: # 生成地址
{
"name": "runoob",
"version": "1.0.0",
"description": "Node.js 测试模块(www.runoob.com)",
……
}
Is this ok? (yes) yes
上記の情報は、状況に応じて入力する必要があります。 最後に「yes」と入力すると、package.json ファイルが生成されます。
次に、次のコマンドを使用して、npm リポジトリにユーザーを登録します (電子メール アドレスを使用)。
$ npm adduser Username: mcmohd Password: Email: (this IS public) mcmohd@gmail.com
次に、次のコマンドを使用してモジュールを公開します。
$ npm public
上記の手順を正しく実行した場合は、npm を使用して他のモジュールと同様にインストールできます。
バージョン番号は、NPM を使用してコードをダウンロードする場合と公開する場合の両方で変更されます。 NPM はセマンティック バージョン番号を使用してコードを管理します。ここで簡単に紹介します。
セマンティック バージョン番号は 3 桁の X.Y.Z に分割され、それぞれメジャー バージョン番号、マイナー バージョン番号、パッチ バージョン番号を表します。 コードが変更されると、次の原則に従ってバージョン番号が更新されます。
バージョン番号にこの保証が与えられた後は、サードパーティのパッケージの依存関係を宣言するときに、固定のバージョン番号に依存するだけでなく、特定の範囲のバージョン番号にも依存する可能性があります。 たとえば、「argv」: 「0.0.x」は、0.0.x シリーズの argv の最新バージョンに依存していることを示します。
NPM でサポートされているすべてのバージョン番号範囲の指定方法については、公式ドキュメントを参照してください。 。
この章で紹介した部分以外にも、NPM は多くの機能を提供しており、package.json には他にも便利なフィールドが多数あります。
npmjs.org/doc/ で公式ドキュメントを参照できるほか、一般的な NPM コマンドをいくつか紹介します。
NPM には、インストールやパブリッシュなどの多くのコマンドが用意されており、すべてのコマンドを表示するには npm help を使用します。
NPM には、install や publish などの多くのコマンドが用意されています。すべてのコマンドを表示するには、npm help を使用します。
npm help <command> を使用すると、npm help install などのコマンドの詳細なヘルプが表示されます。
package.json が配置されているディレクトリで、npm install . -g を使用して、現在のコマンドライン プログラムをローカルにインストールします。リリース前のローカルテスト用。
npm update <package> を使用して、現在のディレクトリの node_modules サブディレクトリ内の対応するモジュールを最新バージョンに更新します。
npm update <package> -g を使用して、グローバルにインストールされている対応するコマンドライン プログラムを最新バージョンに更新します。
npmキャッシュクリアを使用すると、NPMローカルキャッシュをクリアできます。これは、同じバージョン番号のコードの新しいバージョンをリリースする人に対処するために使用されます。
公開したバージョン コードを非公開にするには、npm unpublish <package>@<version> を使用します。