クイックリファレンス

package.json チートシート(npm・Node.js 設定ファイル)

Node.js / npm プロジェクトの中核設定ファイル package.json の完全リファレンス。メタデータ、依存関係の種類、バージョン範囲指定(^/~)、npm scripts を整理。

· 公開日

package.json は Node.js プロジェクトおよび npm パッケージにおける中核的なマニフェストファイルであり、メタデータ、依存関係、実行スクリプト、ビルド設定を一元管理します。

基本マニフェスト構造(Basic Manifest Structure)

{
  "name": "my-awesome-package",
  "version": "1.0.0",
  "description": "Production-grade utility library",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "bin": {
    "mycli": "./bin/cli.js"
  },
  "keywords": ["node", "api", "tools"],
  "author": "Jane Doe <[email protected]>",
  "license": "MIT",
  "engines": {
    "node": ">=18.0.0"
  }
}

依存関係の種類とバージョン指定ルール(SemVer)

{
  "dependencies": {
    "express": "^4.19.2",
    "lodash": "~4.17.21"
  },
  "devDependencies": {
    "eslint": "^9.0.0",
    "typescript": "^5.4.5"
  },
  "peerDependencies": {
    "react": ">=18.0.0"
  },
  "optionalDependencies": {
    "fsevents": "^2.3.3"
  }
}
  • 1.2.3: Exact version match.
  • ^1.2.3: Compatible with version (1.x.x).
  • ~1.2.3: Patch upgrades only (1.2.x).
  • *: Any available version.

npm Scripts と設定変数(Configuration Variables)

{
  "config": {
    "port": "8080"
  },
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "tsc -p tsconfig.build.json",
    "start": "node dist/index.js",
    "test": "vitest run",
    "server": "echo Server port: $npm_package_config_port"
  }
}

関連するチートシート

関連テーマ