LoginSignup
5
4

More than 3 years have passed since last update.

package.json について概要だったり、詳細だったり

Last updated at Posted at 2019-08-22

package.jsonってなんだ?となったので、備忘録。随時更新します。

関連用語

  • Node.js・・・サーバサイドで動く javascript
  • npm・・・javascript のモジュールを管理するツール。ruby でいう bundler のようなもの。
  • yarn・・・npm と同じ javascript のモジュールを管理するツール。npm よりインストール速度が早いらしい

概要

パッケージの依存関係を記した json ファイル。rails で使用する Gemfile のようなもの

作り方

npm init でカレントディレクトリに作成される。

結果

カレントディレクトリ jstest で npm init を打って、何も入力せずに Enter 連打すると以下のようになる

コマンド結果
$ npm init
・・・略・・・
name: (jstest)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
/package.json:

{
  "name": "jstest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Is this ok? (yes) yes
package.json
{
  "name": "jstest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

中身

name

パッケージ名。必須。name と version で一意にする。他のライブラリと被ったらダメ

"name": "jstest"

version

パッケージのバージョン。必須。パッケージ更新時は version も更新する

"version": "1.0.0"

description

パッケージについての説明

"description": ""

main

パッケージ内で最初に呼ばれるモジュール。今回で言えば、パッケージ jstest を require した時に、index.js 内で export しているオブジェクトが返る

"main": "index.js"

scripts

シェルスクリプト、エイリアスコマンドを指定できる
test、start などの予約語を key 名に指定した場合、npm key 名でコマンド実行できる。(他の予約語はなにがあるか分からない・・・)

"scripts": {
    "test": "echo test"
  }

予約語以外を key 名に指定した場合、npm run key 名でコマンド実行できる。devDependencies にモジュール指定しておけば、PATH が自動的に通る

"scripts": {
    "webpack": "node_modules/.bin/webpack -d"
  }

と、書かなくてもモジュール指定しておけば以下のように書ける

"scripts": {
    "webpack": "webpack -d"
  }
"devDependencies": {
    "webpack": "^4.34.0"
 }

author

著者を一人書く。プロジェクト開発ではあまり気にしなくていいかも

author: "Barney Rubble <b@rubble.com> (http://barnyrubble.tumblr.com/)"

license

パッケージのライセンス。これも気にしなくていいかも。ISC はゆるいライセンスで、コピー、改変、配布などを許可するものらしい

"license": "ISC"

参考

公式ドキュメント
package.json の scripts についての備忘録

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4