0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【pnpm】package.json で pnpm のバージョンを指定する場合、engines と packageManager どっちに書くかね

Last updated at Posted at 2024-07-06

以前、こういう試みをした時に、たくさんのリポジトリのパッケージマネージャーを pnpm に置き換えていくことをしました。

その時に、package.json で使用するパッケージマネージャー及びバージョンを指定する場合、複数のやり方があることを知りました。

engines フィールドに書く

package.json
"engines": {
  "pnpm": ">=8"
}

こちらに書くメリットとしては、

  • 柔軟なバージョン指定が可能。例えば、">="を使って最小バージョンを指定できる
  • Node.jsのバージョンなど、他のエンジン要件も同時に指定できる

などでしょうか。

packageManager フィールドに書く

package.json
"packageManager": "pnpm@9.4.0"

メリットとしては、

  • プロジェクトで使用する正確な pnpm バージョンを指定できる
  • Corepack と互換性があり、プロジェクトごとに指定のバージョンの pnpm に切り替えることができる

かなと思います。逆にパッチバージョンまで細かく指定したくないという場合は packageManager ではそれが出来ません。

で、どっちを使いますか

あくまで私のケースですが、結論は packageManager フィールドを使用することにしました。

理由1

チーム管轄で多くのプロジェクトを管轄しており、どうしても pnpm のバージョンが揃わないケースも想定されます。
なので、Corepack でシュッとバージョン切り替え出来るのは大きなメリットと考えます。

FYI:https://nodejs.org/api/corepack.html#supported-package-managers

理由2

GHA のフローの中で pnpm を導入する際、packageManager フィールドを設けておけば、バージョンをベタ書きしなくても済みます。

以下、pnpm オフィシャルで紹介されている actions の書き方の引用ですが、

.github/workflows/NAME.yml
name: pnpm Example Workflow
on:
  push:

jobs:
  build:
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        node-version: [20]
    steps:
    - uses: actions/checkout@v4
    - name: Install pnpm
      uses: pnpm/action-setup@v4
      with:
        version: 9
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'pnpm'
    - name: Install dependencies
      run: pnpm install

で、pnpm/action-setup の README を見ると、

version
Version of pnpm to install.

Optional when there is a packageManager field in the package.json.

otherwise, this field is required It supports npm versioning scheme, it could be an exact version (such as 6.24.1), or a version range (such as 6, 6.x.x, 6.24.x, ^6.24.1, *, etc.), or latest.

version 指定は packageManager フィールドがない時以外は必須なので、
つまり packageManager の内容を参照させることが出来るので、日々のバージョン更新も package.json だけ更新すれば良くなります。

雑ですが、こんな感じ

.github/workflows/NAME.yml
name: pnpm Example Workflow
on:
  push:

jobs:
  build:
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        node-version: [20]
    steps:
    - uses: actions/checkout@v4
    - name: Install pnpm
      uses: pnpm/action-setup@v4
      with:
        package_json_file: package.json
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'pnpm'
    - name: Install dependencies
      run: pnpm install

以上により、現状は packageManager で運用していこうかなあという感じです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?