[GitHub Packages] パッケージ公開するため必要準備

2 min read
GitHub Packages
hiroweb developer

リポジトリ内の準備

package.json に以下のように publishConfig を設定する。

# package.json
"publishConfig": {
  "registry": "https://npm.pkg.github.com/"
},

.npmrc にregistryの設定を追加して対応も可能(今回は割愛)。

パーソナルアクセストークンの準備

ローカルから手動でnpm publishするためには、パーソナルアクセストークンが必要になる。

アクセストークンを作成する際は、read:packagewrite:packageの権限で作成すること。

ローカルの設定

作成したアクセストークンをローカルの~/.npmrcに定義する。

# ~/.npmrc
//npm.pkg.github.com/:_authToken=作成したアクセストークン

ここまでの準備が整ったら、npm publishでパッケージを公開できるようになっている。

npm publish

npm publishを実行し、リモートのバージョンとの競合がなければ以下のようなログが出力される。

npm notice
npm notice 📦  @example/package@1.0.0
npm notice === Tarball Contents ===
npm notice 879B   README.md

...省略...

npm notice 4.4kB  dist/index.d.ts
npm notice 4.6kB  dist/index.js
npm notice 1.4kB  package.json
npm notice === Tarball Details ===
npm notice name:          @example/package
npm notice version:       1.0.0
npm notice filename:      example-package-1.0.0.tgz
npm notice package size:  100.4 kB
npm notice unpacked size: 218.4 kB
npm notice shasum:        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
npm notice integrity:     sha512-xxxxxx[...]xxxxxxxxxx==
npm notice total files:   10
npm notice
npm notice Publishing to https://npm.pkg.github.com/ with tag latest and default access
+ @example/package@1.0.0