Publishing NPM packages and switching registry mirrors

Publish your code to NPM and learn how registry mirrors in China can speed up dependency downloads.

🎯 Introduction

In this article, you will learn to package reusable code and publish it to NPM for developers worldwide. You will also learn how to switch registry mirrors to address slow dependency downloads in mainland China.


🧱 What is an NPM registry mirror?

When you run npm install, NPM downloads packages from the official registry by default. Network conditions can make access from mainland China slow or unreliable.

A registry mirror is a copy of the official registry hosted on servers in China. Switching to a nearby mirror can make downloads much faster.

Common registry mirrors in China include:

MirrorURL
Taobao NPM mirrorhttps://registry.npmmirror.com
Tencent Cloud mirrorhttps://mirrors.cloud.tencent.com/npm
Huawei Cloud mirrorhttps://repo.huaweicloud.com/repository/npm

🛠 Switch registry mirrors

Check the current registry

npm config get registry

Use a mirror for one command

npm install dayjs --registry=https://registry.npmmirror.com

Change the default registry

npm config set registry https://registry.npmmirror.com

Run npm config get registry again to check that it shows the new URL.

To use a mirror for just one project, create an .npmrc file in its root directory:
.npmrc
registry=https://registry.npmmirror.com
This applies the mirror only to the current project, without changing the global configuration.

⚠️ Switch back to the official registry before publishing

To publish to the official NPM registry, switch back to it first:

npm config set registry https://registry.npmjs.org

Mirrors in China are usually read-only and do not support publishing.


🚀 Publish your first NPM package

Step 1: Create an NPM account

Visit https://www.npmjs.com, create an account, and verify your email address.

Step 2: Log in to NPM

Run this in your terminal:

npm login

Follow the prompts to provide your username, password, and email address. A successful login displays Logged in as xxx.

Step 3: Prepare the project

Create a folder and initialize a project:

mkdir my-first-npm-package
cd my-first-npm-package
npm init -y
Before publishing, make sure the name in package.json is available on NPM. Search for it at https://www.npmjs.com.

Step 4: Write the entry point

Create index.js:

index.js
function sayHello(name) {
    return `Hello, ${name}!`;
}

module.exports = { sayHello };

Step 5: Publish

npm publish

If publishing succeeds, you will see a confirmation and your package will be available on NPM.

For your first release, consider setting version to 1.0.0 or 0.1.0. After updating the code, increase version before publishing again.

📦 Update a published package

After changing the code, update the version before publishing. You can use:

npm version patch   # Increment patch, e.g. 1.0.0 -> 1.0.1
npm version minor   # Increment minor, e.g. 1.0.0 -> 1.1.0
npm version major   # Increment major, e.g. 1.0.0 -> 2.0.0

Then run:

npm publish

❓ Questions and answers

Q1: How does a mirror relate to the official NPM registry?

Mirrors regularly synchronize with the official registry, so their contents are largely the same. Publishing, however, requires the official registry.

Q2: Why can't I publish through a mirror in China?

These mirrors usually support downloads only and do not provide write access. Switch to https://registry.npmjs.org and log in before publishing.

Q3: What is .npmrc for?

It configures NPM behavior for a project, such as its registry mirror, proxy, and dependency installation settings.

Q4: What does npm version patch do?

It increments the patch number in package.json, for example from 1.0.0 to 1.0.1, and automatically creates a Git commit if the project is in a Git repository.


🧪 Practice

  1. Check the NPM registry configured on your computer:
# Enter your command here
  1. Try switching to the Taobao mirror and installing a package to compare download speed.
  2. (Optional) Create an NPM account and try publishing a package containing a simple utility function.

🎉 You now know how to switch registry mirrors and publish an NPM package! Next, we will explore package-lock.json and npm ci for more reliable dependency management.