LoginSignup
7
8

More than 1 year has passed since last update.

npm installとpackage.jsonのnpm-scriptsの意味と依存関係等など

Last updated at Posted at 2021-06-17

Node package manager で、なんとなく打っているコマンドのそれぞれの意味を丁寧に追いかけた。以下も参考。

npm install

npm install, npm i と package.json をドキュメントに沿って見る。

npmとpackage.json

どう使うのか
npm installとpackage.jsonが存在するディレクトリでコマンドを打てば、package.jsonに記述されている依存パッケージを自動でインストールしてくれる
npm installをするとnode_modulesというディレクトリがnpm installを実行したディレクトリに作成され、npmを利用してインストールしたパッケージは、この中に格納される

package.jsonを概念的に理解する

scripts は簡単に言えばコマンドのエイリアスであり、任意のコマンド(i.e. コマンドラインのコマンド)に名前をつけることができる。例えば以下のような形である。

  "scripts": {
    "start": "node index.js",
    "lint": "eslint"
  },

ここに記載された script はnpm run で実行できる。例えば上のlintはnpm run lintで実行できる。

npm-scripts

{
  "scripts": {
    "myFirstScript": "echo done"
  },
}

は、例えば npm run myFirstScript で実行できるという話。

package.json を作る

npm init

で例えば以下対話式でファイルができる。

C:\git\js-playground\nodejs\script>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 init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (script) mytest
version: (1.0.0)
description: mytest package json
entry point: (index.js)
test command: test
git repository:
keywords:
author: yamada_n
license: (ISC)
About to write to C:\git\js-playground\nodejs\script\package.json:

{
  "name": "mytest",
  "version": "1.0.0",
  "description": "mytest package json",
  "main": "index.js",
  "scripts": {
    "test": "test"
  },
  "author": "yamada_n",
  "license": "ISC"
}


Is this OK? (yes) yes
package.json
{
  "name": "mytest",
  "version": "1.0.0",
  "description": "mytest package json",
  "main": "index.js",
  "scripts": {
    "test": "test"
  },
  "author": "yamada_n",
  "license": "ISC"
}

先程のとおり scriptsmyFirstScript を足してみる。

package.json
{
  "name": "mytest",
  "version": "1.0.0",
  "description": "mytest package json",
  "main": "index.js",
  "scripts": {
    "test": "test",
    "myFirstScript": "echo done"
  },
  "author": "yamada_n",
  "license": "ISC"
}

npm run myFirstScript は以下のようになった。

C:\git\js-playground\nodejs\script>npm run myFirstScript

> mytest@1.0.0 myFirstScript
> echo done

done

Dependencies

で、依存関係が書かれているのがここ。
npm install typescript などと打つと

C:\git\js-playground\nodejs\script>npm install typescript

added 1 package, and audited 2 packages in 4s

found 0 vulnerabilities
package.json
  "dependencies": {
    "typescript": "^4.3.4"
  }

package-lock.json

もうひとつ、package-lock.json とは何か。

npm と npx

関連

npm と yarn と pnpm 比較(2021年4月版) - Qiita
10 npm Commands that every developer must know
package.json file explained
npmは本当に「Node Package Manager」の意味なのか

これでインストール時のあれやこれやの意味がわかる。
以上参考になればさいわいです。

7
8
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
7
8