Help Center/ CodeArts Artifact/ Best Practices/ Publishing/Obtaining an npm Package via a Build Task
Updated on 2026-03-13 GMT+08:00

Publishing/Obtaining an npm Package via a Build Task

During software development, teams often rely on private packages to protect intellectual property and promote code reuse. However, fetching dependencies from npm registries can introduce permission issues or network latency, leading to build failures. Secure publishing and efficient retrieval of private packages remain critical challenges. This practice describes how to publish a private package to an npm registry via a build task and fetch its dependencies from the registry to complete the build task, resolving the challenges mentioned earlier.

Billing

This function requires CodeArts Repo and CodeArts Build. You need to purchase a CodeArts package.

Prerequisites

  • You already have a project. If no project is available, create one. For example, create a project named project_npm.
  • You have created an npm registry.
  • You have been granted permissions to upload, download, and view packages in the current repository. For details, see Configuring Repository Permissions.

Publishing a Package to an npm Registry

  1. Download the configuration file.

    1. Use your Huawei Cloud account to access self-hosted repos.
    2. Select the target npm registry from the repository list.
    3. Click Tutorial on the right of the page.
    4. In the displayed dialog box, click Download Configuration File.

    5. Save the downloaded npmrc file as an .npmrc file.

  2. Configure a repository.

    1. Log in to the Huawei Cloud console with your Huawei Cloud account.
    2. Click in the upper-left corner and choose Developer Services > CodeArts from the service list.
    3. Click Access Service. The homepage of CodeArts is displayed.
    4. Choose Services > Repo from the top menu bar.
    5. Create a Node.js repository. For details, see Creating a Repository. This procedure uses the Nodejs Webpack Demo template.
    6. Go to the repository and upload the .npmrc file to the root directory of the repository. For details, see Uploading Code Files to CodeArts Repo.

  3. Configure and run a build task.

    1. On the Repo page, select the repository and click Create Build Task in the upper right.

      Select npm and click OK.

    2. Edit the Build with npm action.
      • Select the desired tool version. In this example, nodejs12.7.0 is used.
      • Delete the existing commands and run the following instead.
        export PATH=$PATH:/root/.npm-global/bin
        npm config set strict-ssl false
        npm publish --verbose

    3. Click Save and Run on the right of the page to start the build task.

      After the task is successfully executed, go to the self-hosted repo page and find the uploaded npm package.

Obtaining a Dependency from an npm Registry

The following procedure uses the npm package published in Publishing a Package to an npm Registry as an example to describe how to obtain a dependency from an npm registry.

  1. Configure a repository.

    1. Log in to the Huawei Cloud console with your Huawei Cloud account.
    2. Click in the upper-left corner and choose Developer Services > CodeArts from the service list.
    3. Click Access Service. The homepage of CodeArts is displayed.
    4. Choose Services > Repo from the top menu bar.
    5. Create a Node.js repository. For details, see Creating a Repository. This procedure uses the Nodejs Webpack Demo template.
    6. Obtain the .npmrc file (see Publishing a Package to an npm Registry) and upload it to the root directory of the repository where the npm dependency is to be used.
    7. Find and open the package.json file in the repository, and configure the dependency to the dependencies field. In this document, the value is as follows.
      "@test/vue-demo": "^1.0.0"

  2. Configure and run a build task.

    1. On the Repo page, select the repository and click Create Build Task in the upper right.

      Select Blank Template and click OK.

    2. Add the Build with npm action.

    3. Edit the Build with npm action.
      • Select the desired tool version. In this example, nodejs12.7.0 is used.
      • Delete the existing commands and run the following instead.
        export PATH=$PATH:/root/.npm-global/bin
        npm config set strict-ssl false
        npm install --verbose

  3. Click Save and Run on the right of the page to start the build task.

    After the task is successfully executed, view the task details. If information similar to the following is found in the log, the dependency has been downloaded from the npm registry.

npm Commands

When configuring build tasks, you can also run the following npm commands as required.

  • Delete an existing package from the npm registry.
    npm unpublish @scope/packageName@version
  • Obtain tags.
    npm dist-tag list @scope/packageName
  • Add a tag.
    npm dist-tag add @scope/packageName@version tagName --registry registryUrl --verbose
  • Delete a tag.
    npm dist-tag rm @scope/packageName@version tagName --registry registryUrl --verbose

Command parameter description:

  • scope: path of a self-hosted repo. For details about how to obtain the path, see Publishing a Package to an npm Registry.
  • packageName: the part following scope in the name field of the package.json file.
  • version: value of the version field in the package.json file.
  • registryUrl: URL of the self-hosted repo referenced by scope in the configuration file.
  • tagName: tag name.

The following uses the package published in Publishing a Package to an npm Registry as an example.

  • scope: test
  • packageName: vue-demo
  • version: 1.0.0

The command for deleting this package is as follows.

npm unpublish @test/vue-demo@1.0.0