出典: フリー教科書『ウィキブックス(Wikibooks)』
package.json は、Node.js プロジェクトの設定ファイルであり、プロジェクトのメタデータ、依存関係、スクリプト、バージョン情報などを管理します。このファイルは、Node.js プロジェクトのルートディレクトリに配置され、プロジェクト全体の仕様を定義します。
以下に、package.json の主なプロパティとその意味、および用例を説明します。
| プロパティ
|
意味
|
用例
|
| name
|
パッケージ名。npm のパッケージ名として登録される場合もある。
|
"name": "my-project"
|
| version
|
プロジェクトのバージョンをセマンティックバージョニング形式で指定。
|
"version": "1.0.0"
|
| description
|
プロジェクトの簡単な説明。
|
"description": "A sample Node.js project"
|
| author
|
作者情報。名前やメールアドレスを指定可能。
|
"author": "John Doe <john@example.com>"
|
| license
|
ライセンス情報を指定。
|
"license": "MIT"
|
| private
|
true にすると、プロジェクトが公開されないように保護(npm に公開されない)。
|
"private": true
|
| プロパティ
|
意味
|
用例
|
| scripts
|
コマンドラインで実行するタスクを定義。
|
"scripts": { "start": "node index.js", "test": "jest" }
|
例:
| プロパティ
|
意味
|
用例
|
| dependencies
|
本番環境で必要な依存パッケージを指定。
|
"dependencies": { "express": "^4.18.2" }
|
| devDependencies
|
開発環境のみで必要な依存パッケージを指定(例: テストツール)。
|
"devDependencies": { "jest": "^29.4.3" }
|
| peerDependencies
|
同時にインストールするべき依存パッケージを指定(例: プラグインが必要な場合)。
|
"peerDependencies": { "react": "^17.0.0" }
|
| optionalDependencies
|
インストールが失敗しても無視される依存パッケージを指定。
|
"optionalDependencies": { "fsevents": "^2.3.2" }
|
| プロパティ
|
意味
|
用例
|
| main
|
パッケージのエントリポイントとなるファイルを指定。
|
"main": "index.js"
|
| module
|
ES Modules 用のエントリポイント(main の代わり)。
|
"module": "index.mjs"
|
| exports
|
パッケージ内の公開エントリポイントを細かく指定。
|
"exports": { ".": "./index.js", "./utils": "./utils.js" }
|
| プロパティ
|
意味
|
用例
|
| engines
|
サポートする Node.js のバージョンや npm のバージョンを指定。
|
"engines": { "node": ">=14.0.0" }
|
| engineStrict
|
サポートされていない Node.js バージョンのインストールを禁止する(廃止)。
|
"engineStrict": true
|
| resolutions
|
特定の依存関係のバージョンを強制的に上書き(Yarn 用)。
|
"resolutions": { "lodash": "4.17.21" }
|
| プロパティ
|
意味
|
用例
|
| keywords
|
検索用のキーワードを配列形式で指定。
|
"keywords": ["node", "web", "api"]
|
| homepage
|
プロジェクトのホームページの URL。
|
"homepage": "https://example.com"
|
| repository
|
プロジェクトのリポジトリ情報を指定(URL など)。
|
"repository": { "type": "git", "url": "https://github.com/user/repo.git" }
|
| bugs
|
バグ報告用 URL またはメールアドレス。
|
"bugs": "https://github.com/user/repo/issues"
|
{
"name": "my-project",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.18.2"
}
}
{
"name": "advanced-project",
"version": "1.2.0",
"description": "An advanced Node.js project example",
"main": "index.js",
"module": "index.mjs",
"exports": {
".": "./index.js",
"./utils": "./lib/utils.js"
},
"scripts": {
"start": "node index.js",
"test": "jest"
},
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"jest": "^29.4.3"
},
"keywords": ["node", "javascript", "api"],
"repository": {
"type": "git",
"url": "https://github.com/user/repo.git"
},
"bugs": {
"url": "https://github.com/user/repo/issues"
},
"homepage": "https://example.com",
"license": "MIT",
"engines": {
"node": ">=14.0.0"
}
}