npm Artifact Registry
About 1465 wordsAbout 5 min
Prerequisites
Local Development
Configure Credentials
Three methods are supported:
Paste the following content into the .npmrc file in the same directory as package.json.
Replace <YOUR_TOKEN> with your token, <REPO_URL> with the Artifact Registry address, and <REPO_URL_SUFFIX> with the Artifact Registry address without the protocol prefix.
# Example
# registry=https://npm.cnb.cool/cnb/npm-repo/-/packages/
# always-auth=true
# //npm.cnb.cool/cnb/npm-repo/-/packages/:_authToken=<YOUR_TOKEN>
registry=<REPO_URL>
always-auth=true
//<REPO_URL_SUFFIX>:_authToken=<YOUR_TOKEN>- Set the artifact registry, replace
<REPO_URL>with the Artifact Registry address.
# Example
# npm config set registry https://npm.cnb.cool/cnb/npm-repo/-/packages/
npm config set registry <REPO_URL>- Use the interactive login command. Your username is
cnb, and the password is your access token.
npm loginReplace <YOUR_TOKEN> with your token, <REPO_URL> with the Artifact Registry address, and <REPO_URL_SUFFIX> with the Artifact Registry address without the protocol prefix.
# Example
# npm config set registry https://npm.cnb.cool/cnb/npm-repo/-/packages/
# npm config set //npm.cnb.cool/cnb/npm-repo/-/packages/:_authToken=<YOUR_TOKEN>
npm config set registry <REPO_URL>
npm config set //<REPO_URL_SUFFIX>:_authToken=<YOUR_TOKEN>If your Yarn version is 1.x, you can configure credentials as follows:
Paste the following content into the .npmrc file in the same directory as package.json.
Replace <YOUR_TOKEN> with your token, <REPO_URL> with the Artifact Registry address, and <REPO_URL_SUFFIX> with the Artifact Registry address without the protocol prefix.
# Example
# registry=https://npm.cnb.cool/cnb/npm-repo/-/packages/
# always-auth=true
# //npm.cnb.cool/cnb/npm-repo/-/packages/:_authToken=<YOUR_TOKEN>
registry=<REPO_URL>
always-auth=true
//<REPO_URL_SUFFIX>:_authToken=<YOUR_TOKEN>This command is only suitable for local artifact pushing. Replace <REPO_URL> with the Artifact Registry address.
# Example
# yarn config set registry https://npm.cnb.cool/cnb/npm-repo/-/packages/
yarn config set registry <REPO_URL>
yarn publish
# .....
question npm password: Enter your token hereIf your Yarn version is > 1.x, there are two solutions, but both ultimately modify the .yarnrc.yml file in the same directory as package.json.
Configure credentials in .yarnrc.yml. Replace <REPO_URL> with the Artifact Registry address and <YOUR_TOKEN> with your token.
npmPublishRegistry: "<REPO_URL>"
npmRegistryServer: "<REPO_URL>"
npmRegistries:
"<REPO_URL>":
npmAlwaysAuth: true
npmAuthToken: <YOUR_TOKEN>Replace <REPO_URL> with the Artifact Registry address and <YOUR_TOKEN> with your token.
yarn config set npmPublishRegistry <REPO_URL> # Address for pushing packages
yarn config set npmRegistryServer <REPO_URL> # Address for pulling packages
yarn config set 'npmRegistries[<REPO_URL>].npmAuthToken' <YOUR_TOKEN>This will generate the following .yarnrc.yml:
npmPublishRegistry: "<REPO_URL>"
npmRegistryServer: "<REPO_URL>"
npmRegistries:
"<REPO_URL>":
npmAuthToken: <YOUR_TOKEN>Refer to npm, just replace npm with pnpm in the commands.
Pull Artifacts
Replace <PACKAGE_NAME>, <VERSION>, and <REPO_URL>.
# Example
# npm install express@5.1.0 --registry=https://npm.cnb.cool/cnb/npm-repo/-/packages/
# npm
npm install <PACKAGE_NAME>@<VERSION> --registry=<REPO_URL>
# yarn
yarn add <PACKAGE_NAME>@<VERSION>
# pnpm
pnpm install <PACKAGE_NAME>@<VERSION> --registry=<REPO_URL>Push Artifacts
- Initialize package.json. Replace
<PACKAGE_NAME>and<VERSION>.
{
"name": "<PACKAGE_NAME>",
"version": "<VERSION>",
"description": "",
"main": "index.js",
"author": "",
"license": "MIT"
}- Execute the following command to publish the package. Replace
<REPO_URL>with the Artifact Registry address.
# <REPO_URL> example
# https://npm.cnb.cool/cnb/npm-repo/-/packages/
# npm
npm publish --registry=<REPO_URL>
# yarn version > 1.x
yarn npm publish
# yarn version 1.x
yarn publish --registry=<REPO_URL>
# pnpm
pnpm publish --registry=<REPO_URL>Cloud Native Build
Configure Credentials
Cloud Native Build supports three ways to use tokens. Refer to [Using Tokens in Cloud Native Build and Development] (./intro.md#using-tokens-in-workspaces-and-cloud-native-build). Below are the specific client usage methods.
Paste the following content into the .npmrc file in the same directory as package.json.
Replace <REPO_URL> with the Artifact Registry address and <REPO_URL_SUFFIX> with the Artifact Registry address without the protocol prefix.
# Example
# registry=https://npm.cnb.cool/cnb/npm-repo/-/packages/
# always-auth=true
# //npm.cnb.cool/cnb/npm-repo/-/packages/:_authToken=${CNB_TOKEN}
registry=<REPO_URL>
always-auth=true
# Method 1: Use built-in environment variable CNB_TOKEN
//<REPO_URL_SUFFIX>:_authToken=${CNB_TOKEN}
# Method 2: Directly use, replace <YOUR_TOKEN> with your token
//<REPO_URL_SUFFIX>:_authToken=<YOUR_TOKEN>
# Method 3: Secret Registry, replace <ENV_NAME> with your secret registry variable
# //<REPO_URL_SUFFIX>:_authToken=${<ENV_NAME>}Replace <REPO_URL> with the Artifact Registry address and <REPO_URL_SUFFIX> with the Artifact Registry address without the protocol prefix.
# Example
# npm config set registry https://npm.cnb.cool/cnb/npm-repo/-/packages/
# npm config set //npm.cnb.cool/cnb/npm-repo/-/packages/:_authToken=${CNB_TOKEN}
npm config set registry <REPO_URL>
# Method 1: Use built-in environment variable CNB_TOKEN
npm config set //<REPO_URL_SUFFIX>:_authToken=${CNB_TOKEN}
# Method 2: Directly use, replace <YOUR_TOKEN> with your token
# npm config set //<REPO_URL>:_authToken=<YOUR_TOKEN>
# Method 3: Secret Registry, replace <ENV_NAME> with your secret registry variable
# npm config set //<REPO_URL>:_authToken=${<ENV_NAME>}If your Yarn version is 1.x, you can configure credentials as follows:
Paste the following content into the .npmrc file in the same directory as package.json. Replace <REPO_URL> with the Artifact Registry address and <REPO_URL_SUFFIX> with the Artifact Registry address without the protocol prefix.
# Example
# registry=https://npm.cnb.cool/cnb/npm-repo/-/packages/
# always-auth=true
# //npm.cnb.cool/cnb/npm-repo/-/packages/:_authToken=${CNB_TOKEN}
registry=<REPO_URL>
always-auth=true
# Method 1: Use built-in environment variable CNB_TOKEN
//<REPO_URL_SUFFIX>:_authToken=${CNB_TOKEN}
# Method 2: Directly use, replace <YOUR_TOKEN> with your token
//<REPO_URL_SUFFIX>:_authToken=<YOUR_TOKEN>
# Method 3: Secret Registry, replace <ENV_NAME> with your secret registry variable
# //<REPO_URL_SUFFIX>:_authToken=${<ENV_NAME>}If your Yarn version is > 1.x, there are two solutions, but both ultimately modify the .yarnrc.yml file in the same directory as package.json.
npmPublishRegistry: "<REPO_URL>" # Address for pushing packages
npmRegistryServer: "<REPO_URL>" # Address for pulling packages
npmRegistries: # Configure credentials for each Artifact Registry
"<REPO_URL>":
npmAlwaysAuth: true
# Method 1: Use built-in environment variable CNB_TOKEN
npmAuthToken: ${CNB_TOKEN}
# Method 2: Directly use, replace <YOUR_TOKEN> with your token
# npmAuthToken: <YOUR_TOKEN>
# Method 3: Secret Registry, replace <ENV_NAME> with your secret registry variable
# npmAuthToken: ${<ENV_NAME>}yarn config set npmPublishRegistry <REPO_URL> # Address for pushing packages
yarn config set npmRegistryServer <REPO_URL> # Address for pulling packages
# Method 1: Use built-in environment variable CNB_TOKEN
yarn config set 'npmRegistries[<REPO_URL>].npmAuthToken' ${CNB_TOKEN}
# Method 2: Directly use, replace <YOUR_TOKEN> with your token
# yarn config set 'npmRegistries[<REPO_URL>].npmAuthToken' <YOUR_TOKEN>
# Method 3: Secret Registry, replace <ENV_NAME> with your secret registry variable
# yarn config set 'npmRegistries[<REPO_URL>].npmAuthToken' ${<ENV_NAME>}Refer to npm, just replace npm with pnpm in the commands.
Pull Artifacts
main:
push:
- docker:
image: node:22
stages:
- name: npm install
script:
- npm config set registry <REPO_URL>
- npm config set //<REPO_URL_SUFFIX>:_authToken=${CNB_TOKEN}
- npm install --registry=<REPO_URL>If credentials are already configured via
.npmrc, you can usenpm install --registry=<REPO_URL>directly instead of the three commands above.
main:
push:
- docker:
image: node:22
stages:
- name: yarn install
script:
- npm install -g corepack
- corepack enable
- corepack prepare yarn@1.22.22 --activate
# If Yarn version > 1.x and using command-line setup, include the following two lines
# - yarn config set npmRegistryServer <REPO_URL>
# - yarn config set 'npmRegistries[<REPO_URL>].npmAuthToken' ${CNB_TOKEN}
- yarn installRefer to npm, just replace npm with pnpm in the commands.
Push Artifacts
The core pattern: update the version number in tag_push event and publish. Below is an example using npm (recommended tencentcom/npm plugin):
$:
tag_push:
- docker:
image: node:22
stages:
- name: update version
script: npm version $CNB_BRANCH --no-git-tag-version
- name: npm publish
image: tencentcom/npm
settings:
username: $CNB_TOKEN_USER_NAME
token: $CNB_TOKEN
email: $CNB_COMMITTER_EMAIL
registry: <REPO_URL>
folder: ./
fail_on_version_conflict: trueFor other package managers (replace the corresponding npm stages above):
- Yarn 1.x: Install
yarn@1.x, update version withyarn version --new-version=$CNB_BRANCH, publish withyarn publish. - Yarn > 1.x: Install
yarn@4.x, update version withyarn version $CNB_BRANCH, publish withyarn npm publish. - pnpm: Install
pnpm, update version withpnpm version $CNB_BRANCH --no-git-tag-version, configure credentials withpnpm config set, thenpnpm publish --no-git-checksto publish.
Workspaces
Configure Credentials
Same as Cloud Native Build Configure Credentials
Configure Development Image
$:
vscode:
- docker:
image: node:22You can execute the following command to select a package manager. Replace <NAME> and <VERSION> with your package manager name and version.
npm install -g corepack
corepack enable
# Example: corepack prepare yarn@1.22.22 --activate
corepack prepare <NAME>@<VERSION> --activatePull Artifacts
Same as Local Development Pull Artifacts
Push Artifacts
Same as Local Development Push Artifacts
More Usage
For more npm usage, please refer to the npm official documentation.