CLOVER🍀

That was when it all began.

npmでプロゞェクトの䟝存関係の確認や、䟝存パッケヌゞのバヌゞョン確認などを行っおみる

これは、なにをしたくお曞いたもの

以前、npm installnpm iで䜿うバヌゞョンに぀いお調べおみたした。

npm installなどで使うバージョンがよくわからなかったので、調べてみました - CLOVER🍀

なのですが。npmプロゞェクトが䟝存しおいるパッケヌゞを衚瀺したり、より新しいバヌゞョンがリリヌスされおいるかどうかなどはどうやっお
確認したらいいのかなず思っお調べおみるず。

npmコマンドでできそうだったので、たずめおおくこずにしたす。

結論を蚀うず、npm ls、npm outdated、npm updateなどを䜿うず良さそうです。

環境

今回の環境は、こちら。

$ node --version
v16.18.1


$ npm --version
8.19.2

準備

準備ずしお、npmプロゞェクトを䜜成しお珟時点では少し叀いパッケヌゞを䟝存関係に远加しおみたす。

$ npm init -y


$ npm i express@4.17.0
$ npm i winston@3.8.0
$ npm i -D @types/node@16.18.0
$ npm i -D typescript@4.8.2

package.jsonの䟝存関係は、このようになりたした。

  "dependencies": {
    "express": "^4.17.0",
    "winston": "^3.8.0"
  },
  "devDependencies": {
    "@types/node": "^16.18.0",
    "typescript": "^4.8.2"
  }

npmプロゞェクトの䟝存関係を衚瀺する

npmプロゞェクトが䟝存しおいるパッケヌゞを衚瀺するには、npm lsnpm listを䜿うず良さそうです。

This command will print to stdout all the versions of packages that are installed, as well as their dependencies when --all is specified, in a tree structure.

npm-ls | npm Docs

むンストヌルされおいる䟝存関係を衚瀺しおくれるコマンドのようです。

実行しおみたす。

$ npm ls
npm-dependencies@1.0.0 /path/to/my-npm-project
├── @types/node@16.18.0
├── express@4.17.0
├── typescript@4.8.2
└── winston@3.8.0

珟圚のプロゞェクトの䟝存関係が衚瀺されたした。

パッケヌゞを盎接指定するこずもできたす。

$ npm ls express
npm-dependencies@1.0.0 /path/to/my-npm-project
└── express@4.17.0

デフォルトだず1階局しか衚瀺しないようなので、䟝存しおいるパッケヌゞが掚移的に他のパッケヌゞに䟝存しおいる堎合を衚瀺するような
ケヌスでは、--depthオプションを䜿いたす。

2階局たで衚瀺しおみたす。

$ npm ls --depth 2
npm-dependencies@1.0.0 /path/to/my-npm-project
├── @types/node@16.18.0
├─┬ express@4.17.0
│ ├─┬ accepts@1.3.8
│ │ ├── mime-types@2.1.35
│ │ └── negotiator@0.6.3
│ ├── array-flatten@1.1.1
│ ├─┬ body-parser@1.19.0
│ │ ├── bytes@3.1.0
│ │ ├── content-type@1.0.4 deduped
│ │ ├── debug@2.6.9 deduped
│ │ ├── depd@1.1.2 deduped
│ │ ├── http-errors@1.7.2
│ │ ├── iconv-lite@0.4.24
│ │ ├── on-finished@2.3.0 deduped
│ │ ├── qs@6.7.0 deduped
│ │ ├── raw-body@2.4.0
│ │ └── type-is@1.6.18 deduped
│ ├─┬ content-disposition@0.5.3
│ │ └── safe-buffer@5.1.2 deduped

〜省略〜

デフォルトでは、--depthに1が指定された状態で実行しおいるようです。

党階局を衚瀺する堎合は、--allオプションを䜿いたす。

$ npm ls --all
npm-dependencies@1.0.0 /path/to/my-npm-project
├── @types/node@16.18.0
├─┬ express@4.17.0
│ ├─┬ accepts@1.3.8
│ │ ├─┬ mime-types@2.1.35
│ │ │ └── mime-db@1.52.0
│ │ └── negotiator@0.6.3
│ ├── array-flatten@1.1.1
│ ├─┬ body-parser@1.19.0
│ │ ├── bytes@3.1.0
│ │ ├── content-type@1.0.4 deduped
│ │ ├── debug@2.6.9 deduped
│ │ ├── depd@1.1.2 deduped
│ │ ├─┬ http-errors@1.7.2
│ │ │ ├── depd@1.1.2 deduped
│ │ │ ├── inherits@2.0.3 deduped
│ │ │ ├── setprototypeof@1.1.1 deduped
│ │ │ ├── statuses@1.5.0 deduped
│ │ │ └── toidentifier@1.0.0
│ │ ├─┬ iconv-lite@0.4.24
│ │ │ └── safer-buffer@2.1.2
│ │ ├── on-finished@2.3.0 deduped
│ │ ├── qs@6.7.0 deduped

〜省略〜

重耇しおいるものは、dedupedが付いおいるみたいですね。

新しいバヌゞョンのパッケヌゞがリリヌスされおいるかどうか確認する

珟圚のプロゞェクトが䟝存しおいるnpmパッケヌゞの䞭に、より新しいバヌゞョンのものがリリヌスされおいるかどうかを確認するには
npm outdatedを䜿甚したす。

npm-outdated | npm Docs

実行しおみたす。

$ npm outdated
Package      Current   Wanted   Latest  Location                  Depended by
@types/node  16.18.0  16.18.3  18.11.9  node_modules/@types/node  npm-dependencies
express       4.17.0   4.18.2   4.18.2  node_modules/express      npm-dependencies
typescript     4.8.2    4.9.3    4.9.3  node_modules/typescript   npm-dependencies
winston        3.8.0    3.8.2    3.8.2  node_modules/winston      npm-dependencies

「Current」は珟圚のバヌゞョンです。「Wanted」は、package.jsonで指定されたsemverの範囲を満たすもので最も新しいバヌゞョンを
衚瀺したす。「Latest」はパッケヌゞの最新のバヌゞョンですね。

「Current」ず「Latest」が䞀臎しおいる堎合は、結果に衚瀺されないようです。

今回、意図的に少し叀いパッケヌゞをむンストヌルしおいたすが、ほずんどのものは「Wanted」ず「Latest」が䞀臎しおいたす。
@types/nodeのみ、メゞャヌバヌゞョンが異なるのでv16の範囲が「Wanted」ずなり、「Latest」ず差が出おいたす。

  "dependencies": {
    "express": "^4.17.0",
    "winston": "^3.8.0"
  },
  "devDependencies": {
    "@types/node": "^16.18.0",
    "typescript": "^4.8.2"
  }

䟝存しおいるパッケヌゞを新しいバヌゞョンに曎新する

npm outdatedで新しいバヌゞョンのパッケヌゞがリリヌスされおいるこずを確認できたら、アップデヌトを怜蚎するわけですが。

これは、npm updateで行えるようです。

npm-update | npm Docs

npm updateでは、パッケヌゞの最新版たたはsemverを満たす範囲でパッケヌゞをアップデヌトしたす。

This command will update all the packages listed to the latest version (specified by the tag config), respecting the semver constraints of both your package and its dependencies (if they also require the same package).

䞍足しおいるパッケヌゞがある堎合は、同時に远加しおくれるようです。

詊しおみたしょう。npm outdatedの結果は以䞋でした。

$ npm outdated
Package      Current   Wanted   Latest  Location                  Depended by
@types/node  16.18.0  16.18.3  18.11.9  node_modules/@types/node  npm-dependencies
express       4.17.0   4.18.2   4.18.2  node_modules/express      npm-dependencies
typescript     4.8.2    4.9.3    4.9.3  node_modules/typescript   npm-dependencies
winston        3.8.0    3.8.2    3.8.2  node_modules/winston      npm-dependencies

最初にどれくらいの倉曎量があるのかを確認しおみたす。これには--dry-runオプションを䜿いたす。

$ npm update --dry-run

added 7 packages, removed 3 packages, and changed 21 packages in 757ms

8 packages are looking for funding
  run `npm fund` for details

あんたりよくわかりたせんが 。

では、曎新しおみたす。

$ npm update

added 7 packages, removed 1 package, changed 23 packages, and audited 87 packages in 5s

8 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

npm lsの結果。

$ npm ls
npm-dependencies@1.0.0 /path/to/my-npm-project
├── @types/node@16.18.3
├── express@4.18.2
├── typescript@4.9.3
└── winston@3.8.2

䟝存パッケヌゞがアップデヌトされたようです。

npm outdatedも確認しおみたしょう。衚瀺されるのが@types/nodeのみになりたした。

$ npm outdated
Package      Current   Wanted   Latest  Location                  Depended by
@types/node  16.18.3  16.18.3  18.11.9  node_modules/@types/node  npm-dependencies

これは、以䞋の蚘述なのでv16の範囲でしか曎新されないからですね。

    "@types/node": "^16.18.0",

メゞャヌバヌゞョンが倉わるような堎合はリリヌスノヌト等それなりに確認するこずもあるず思うので、semverの範囲を守るのは劥圓な
感じがしたすね。

semverの範囲よりもさらに曎新したい堎合は、npm installで個々のパッケヌゞを指定しおむンストヌルするこずになりたす。

それでも最新版に䞀括でアップデヌトしたい、ずいう堎合はこちらを䜿うのではないかず思いたす。

npm-check-updates - npm

ずころで、npm updateを実行しおもpackage.jsonが倉わるわけではありたせん。

  "dependencies": {
    "express": "^4.17.0",
    "winston": "^3.8.0"
  },
  "devDependencies": {
    "@types/node": "^16.18.0",
    "typescript": "^4.8.2"
  }

package.jsonも含めお曎新する堎合は、--saveオプションを付䞎したす。

$ npm update --save

こうするず、package.jsonの蚘述も曎新されるようになりたす。

  "dependencies": {
    "express": "^4.18.2",
    "winston": "^3.8.2"
  },
  "devDependencies": {
    "@types/node": "^16.18.3",
    "typescript": "^4.9.3"
  }

こんなずころでしょうか。

たずめ

npmプロゞェクトの䟝存関係を確認したり、新しいバヌゞョンの確認、曎新を行うnpmコマンドを確認しおみたした。

今たであんたり知りたせんでしたが、けっこういろいろできるんですね。芚えおおきたしょう。