Mengelola paket Node.js

Halaman ini menjelaskan tugas-tugas berikut:

  • Melihat dan menghapus paket dan versi paket
  • Melihat, membuat, memperbarui, dan menghapus tag

Sebelum memulai

  1. Jika repositori target tidak ada, buat repositori baru.
  2. Verifikasi bahwa Anda memiliki izin yang diperlukan untuk repositori.
  3. Mengonfigurasi autentikasi untuk npm.
  4. (Opsional) Konfigurasi default untuk perintah gcloud.
  5. Jika Anda menggunakan credential helper npm untuk autentikasi, dapatkan token akses sebelum terhubung ke repositori dengan npm.

Peran yang diperlukan

Untuk mendapatkan izin yang diperlukan untuk mengelola paket, minta administrator Anda untuk memberi Anda peran IAM berikut di repositori:

Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.

Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran kustom atau peran yang telah ditentukan lainnya.

Mendapatkan token akses

Token akses berlaku selama 60 menit. Buat token akses sesaat sebelum menjalankan perintah yang berinteraksi dengan repositori.

Untuk mendapatkan token, gunakan salah satu opsi berikut:

  • Gunakan perintah npx untuk memperbarui token akses.

    1. Pastikan kredensial untuk terhubung ke registry npm publik ada di file konfigurasi npm pengguna Anda, ~/.npmrc.

    2. Jalankan perintah berikut di direktori project Node.js Anda.

      npx google-artifactregistry-auth
      

      Jika repositori Artifact Registry Anda ditetapkan sebagai registry global dan paket Anda tidak diberi cakupan, gunakan perintah berikut agar perintah dapat mendownload credential helper dari registry npm publik, bukan dari repositori Artifact Registry Anda.

      npm_config_registry=https://registry.npmjs.org npx google-artifactregistry-auth
      
  • Tambahkan skrip ke file package.json di project Anda.

    "scripts": {
     "artifactregistry-login": "npx google-artifactregistry-auth"
    }
    

    Jalankan skrip di direktori project Node.js Anda.

    npm run artifactregistry-login
    

Artifact Registry membaca setelan repositori Artifact Registry dalam file .npmrc project Anda dan menggunakannya untuk menambahkan kredensial token ke file .npmrc pengguna Anda. Menyimpan token di file .npmrc pengguna akan mengisolasi kredensial Anda dari kode sumber dan sistem kontrol sumber Anda.

Menambahkan paket

Mode repositori: standar

Anda hanya dapat memublikasikan versi tertentu dari paket satu kali. Ini adalah batasan npm untuk memastikan bahwa konten versi paket yang dipublikasikan selalu sama. Akibatnya, Anda tidak dapat:

  • Menimpa versi paket dengan memublikasikannya lagi ke repositori
  • Menghapus paket atau versinya dari repositori, lalu memublikasikan paket dengan nama dan nomor versi yang sama

Jika Anda tidak menentukan tag saat memublikasikan paket, npm akan menambahkan tag latest. Untuk menyederhanakan penginstalan paket pada tahap pengembangan tertentu, pertimbangkan untuk memublikasikan paket dengan tag, seperti beta atau dev.

Artifact Registry menerapkan nama paket huruf kecil alfanumerik untuk paket npm.

Untuk menambahkan paket:

  1. Pastikan nama paket di package.json menyertakan cakupan yang dikonfigurasi untuk repositori Anda. Contoh berikut menunjukkan paket dengan cakupan dev-repo.

    "name": "@dev-repo/my-package"
    
  2. Jika Anda menggunakan credential helper untuk mengautentikasi dengan token akses, dapatkan token baru.

  3. Tambahkan paket ke repositori. Anda dapat menggunakan perintah npm atau yarn.

    Untuk memberi tag pada paket, sertakan flag --tag dan ganti TAG dengan tag yang ingin Anda gunakan. Jika Anda tidak menyertakan flag --tag, npm akan otomatis menyetel tag ke latest.

    npm publish --tag=TAG
    
    yarn publish --tag TAG
    

Melihat paket dan versi

Mode repositori: standar, jarak jauh, virtual

Untuk mendapatkan informasi paket dengan npm atau yarn:

  1. Jika Anda menggunakan credential helper untuk mengautentikasi dengan token akses, dapatkan token baru.

  2. Jalankan perintah yang sesuai:

    npm view
    
    yarn info
    

To view packages and package versions using the Google Cloud console or gcloud:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of the package.

gcloud

To list packages in a repository, run the following command:

gcloud artifacts packages list [--repository=REPOSITORY] [--location=LOCATION]

Replace the following:

  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. If you configured a default location, then you can omit this flag to use the default.

To view versions of a package, run the following command:

gcloud artifacts versions list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

Replace the following:

  • PACKAGE is the ID of the package or fully qualified identifier for the package.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

Melihat paket dan versi dari konsol Google Cloud atau gcloud CLI hanya tersedia untuk repositori standar dan jarak jauh.

Untuk repositori jarak jauh, daftar yang ditampilkan harus mencakup semua dependensi langsung dan transitif yang di-cache di repositori.

Mencantumkan file

Mode repositori: standar, jarak jauh

Anda dapat mencantumkan file dalam repositori, file dalam semua versi paket yang ditentukan, atau file dalam versi tertentu dari suatu paket.

Untuk semua perintah berikut, Anda dapat menetapkan jumlah maksimum file yang akan ditampilkan dengan menambahkan tanda --limit ke perintah.

Untuk mencantumkan semua file dalam project, repositori, dan lokasi default saat nilai default dikonfigurasi:

gcloud artifacts files list

Untuk mencantumkan file dalam project, repositori, dan lokasi tertentu, jalankan perintah:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION

Untuk mencantumkan file untuk semua versi paket tertentu:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE

Untuk mencantumkan file untuk versi paket tertentu:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE \
    --version=VERSION
Untuk mencantumkan file untuk tag tertentu:

gcloud artifacts files list \
    --project=PROJECT \
    --repository=REPOSITORY \
    --location=LOCATION \
    --package=PACKAGE \
    --tag=TAG

Ganti nilai berikut:

  • LOCATION: lokasi regional atau multi-regional repositori.
  • PROJECT: Google Cloud Project ID Anda. Jika project ID Anda berisi titik dua (:), lihat Project cakupan domain.
  • REPOSITORY: nama repositori tempat penyimpanan image.
  • PACKAGE: nama paket.
  • VERSION: versi paket.
  • TAG: tag yang terkait dengan paket.

Contoh

Pertimbangkan informasi paket berikut:

  • Project: my-project
  • Repository: my-repo
  • Lokasi repositori: us-west1
  • Paket: my-app

Perintah berikut mencantumkan semua file di repositori my-repo di lokasi us-west1 dalam project default:

gcloud artifacts files list \
    --location=us-west1 \
    --repository=my-repo
Perintah berikut mencantumkan file dalam paket versi 1.0.

gcloud artifacts files list \
    --project=my-project \
    --location=us-west1 \
    --repository=my-repo \
    --package=my-app \
    --version=1.0
Perintah berikut mencantumkan file dalam versi paket dengan tag 1.0-dev

gcloud artifacts files list \
    --project=my-project \
    --location=us-west1 \
    --repository=my-repo \
    --package=my-app \
    --tag=1.0-dev

Memberi tag pada paket

Mode repositori: standar

You can view, add, update, and delete tags. Tags can help you manage semantic versions of your packages and streamline installation of packages at a specific stage of development.

For example, you can tag the current release candidate build with rc. Your team can then install the correct version based on the tag instead of a version specifier, and unpublishing unused pre-release versions won't break your dependencies on the release candidate package.

Viewing tags

To view tags for a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. Click the package to view versions and the associated tags.

  3. Select the package version to tag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Type new tags into the field and then click SAVE.

gcloud

Run the command:

gcloud artifacts tags list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

Where

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.

For example, to view tags for the package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags list --package=my-pkg --repository=my-repo

Creating tags

You can create a tag for a specific version of a package.

To tag an existing image in a repository:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. Click the package to view versions of the package.

  3. Select the package version to tag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Type new tags into the field and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags create TAG --package=PACKAGE \
    version=VERSION [--location=LOCATION] [--repository=REPOSITORY]

Where

  • TAG is the tag you want to apply to the package.
  • PACKAGE is the name of the package in the repository.
  • VERSION is version of the package that you want to tag.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.

For example, to create the tag release-candidate for version 1.0.0 of package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags create release-candidate --version=1.0.0 \
    --package=my-pkg --repository=my-repo

Updating tags

You can change a tag associated with a package version.

To change an existing tag:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. Click the package to view versions of the package.

  3. Select the package version with the tag to change.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Edit the tag and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags update TAG --package=PACKAGE \
    version=VERSION [--location=LOCATION] [--repository=REPOSITORY]

Where

  • TAG is the tag you want to apply to the package.
  • PACKAGE is the name of the package in the repository.
  • VERSION is version of the package that you want to tag.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.

For example, to change the tag for version 1.0.0 of package my-package to production in the repository my-repo in the default location, run the command:

gcloud artifacts tags update production --version=1.0.0 \
    --package=my-pkg --repository=my-repo

Untagging package versions

You can remove an existing tag from a package version.

To remove a tag:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. Click the image to view versions of the image.

  3. Select the image version to untag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Delete the tag and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags delete TAG --package=PACKAGE \
    [--location=<LOCATION] [--repository=REPOSITORY]

Where

  • TAG is the tag you want to apply to the package.
  • PACKAGE is the name of the package in the repository.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.

For example, to remove the tag release-candidate from package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags delete release-candidate --package=my-pkg \
    --repository=my-repo

Menginstal paket

Mode repositori: standar, jarak jauh, virtual

Untuk menginstal paket dari repositori paket Node.js:

  1. Jika Anda menggunakan credential helper untuk mengautentikasi dengan token akses, dapatkan token baru.

  2. Gunakan perintah npm install atau yarn add.

    npm

    Untuk menginstal versi dengan tag latest:

    npm install @SCOPE/PACKAGE
    

    Untuk menginstal versi dengan tag yang berbeda:

    npm install @SCOPE/PACKAGE@TAG
    

    Untuk menginstal versi tertentu:

    npm install @SCOPE/PACKAGE@VERSION
    

    benang

    Untuk menginstal versi dengan tag latest:

    yarn add @SCOPE/PACKAGE
    

    Untuk menginstal versi dengan tag yang berbeda:

    yarn add @SCOPE/PACKAGE@TAG
    

    Untuk menginstal versi tertentu:

    yarn add @SCOPE/PACKAGE@VERSION
    

    Ganti nilai berikut:

    • SCOPE adalah cakupan yang terkait dengan repositori. Jika repositori paket Node.js Anda tidak dikonfigurasi dengan cakupan, hapus @SCOPE/ dari perintah.
    • PACKAGE adalah nama paket di repositori.
    • TAG adalah tag untuk versi yang ingin Anda instal.
    • VERSION adalah nomor versi yang ingin Anda instal.

Saat Anda menentukan paket sebagai dependensi di package.json, pastikan Anda menyertakan cakupan untuk repositori. Contoh berikut menunjukkan cakupan @dev-repo untuk paket bernama my-package.

"dependencies": {
  "@dev-repo/my-package": ">=1.0.0"
}

Untuk repositori standar, Anda mendownload paket langsung dari repositori.

Untuk repositori jarak jauh, Anda mendownload salinan paket yang di-cache dan dependensinya. Jika salinan yang di-cache tidak ada, repositori jarak jauh akan mendownload paket dari sumber upstream dan meng-cache-nya sebelum menayangkannya kepada Anda. Anda dapat memverifikasi bahwa repositori jarak jauh mengambil paket dari sumber upstream dengan melihat daftar paket di repositori.

Untuk repositori virtual, Artifact Registry menelusuri repositori upstream untuk paket yang diminta.

  • Repositori jarak jauh upstream akan mendownload dan meng-cache paket yang diminta jika salinan yang di-cache tidak ada. Repositori virtual hanya menayangkan paket yang diminta, tidak menyimpannya.
  • Jika Anda meminta versi yang tersedia di lebih dari satu repositori upstream, Artifact Registry akan memilih repositori upstream yang akan digunakan berdasarkan setelan prioritas yang dikonfigurasi untuk repositori virtual.

Misalnya, pertimbangkan repositori virtual dengan setelan prioritas berikut untuk repositori upstream:

  • main-repo: Prioritas ditetapkan ke 100
  • secondary-repo1: Prioritas ditetapkan ke 80.
  • secondary-repo2: Prioritas ditetapkan ke 80.
  • test-repo: Prioritas ditetapkan ke 20.

main-repo memiliki nilai prioritas tertinggi, sehingga repositori virtual selalu menelusurinya terlebih dahulu.

secondary-repo1 dan secondary-repo2 memiliki prioritas yang ditetapkan ke 80. Jika paket yang diminta tidak tersedia di main-repo, Artifact Registry akan menelusuri repositori ini berikutnya. Karena keduanya memiliki nilai prioritas yang sama, Artifact Registry dapat memilih untuk menayangkan paket dari salah satu repositori jika versi tersedia di keduanya.

test-repo memiliki nilai prioritas terendah dan akan menayangkan artefak yang disimpan jika tidak ada repositori upstream lain yang memilikinya.

Menghapus paket

Mode repositori: standar, jarak jauh

Anda dapat menghapus paket dan semua versinya, atau menghapus versi tertentu.

  • Setelah menghapus paket, Anda tidak dapat mengurungkan tindakan tersebut.
  • Untuk repositori jarak jauh, hanya salinan paket yang di-cache yang dihapus. Sumber upstream tidak terpengaruh. Jika Anda menghapus paket yang di-cache, Artifact Registry akan mendownload dan men-cache-nya lagi saat repositori menerima permintaan untuk versi paket yang sama pada waktu berikutnya.

Setelah versi paket dipublikasikan, Anda tidak dapat memublikasikan ulang paket dengan kombinasi nama dan versi yang sama, meskipun setelah menghapus versi tersebut. Ini adalah batasan npm untuk memastikan bahwa konten versi paket yang dipublikasikan selalu sama.

Jika Anda ingin mendorong pengguna menginstal versi paket yang diupdate, gunakan perintah npm deprecate untuk menandai versi lama paket sebagai tidak digunakan lagi. Saat pengguna mencoba menginstal paket yang tidak digunakan lagi, Artifact Registry akan menampilkan peringatan penghentian penggunaan.

Before you delete a package or package version, verify that any you have communicated or addressed any important dependencies on it.

To delete a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Select the package that you want to delete.

  4. Click DELETE.

  5. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts packages delete PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] [--async]

Replace the following:

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

To delete versions of a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    Buka halaman Repositori

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of that package.

  4. Select versions that you want to delete.

  5. Click DELETE.

  6. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts versions delete VERSION \
    --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] \
    [--async]

Replace the following:

  • VERSION is the name of the version to delete.
  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

Langkah berikutnya