Problem


A few months ago, my Angular project on Azure DevOps started to have build failure very often. Error message is "npm ERR! network read ECONNRESET", which indicates npm can't connect to internet. This problem does not happen every time, usually re-run failed jobs several times can produce a good build. But still, this is very annoying. So, I took some time to investigate and solve it.

Root Cause


First, I thought it was a IP rate limit by npm that reject Azure pipeline's agent. So I set up a VM on Azure as a self-host build agent. However, the problem continues.

Then, I setup the same build agent on my laptop, which is outside of Azure environment, the build succeed every time. It seems to be npm rejecting the entire Azure IP address. But I can open https://registry.npmjs.org/ without any problem on my Azure VM, how come the IP limit?

Finally, I added verbose log to the npm install task in my pipeline yaml file like this:

jobs:
- job: Angular
  pool:
    vmImage: 'ubuntu-latest'
  steps:

  - task: Npm@1
    displayName: 'npm install'
    inputs:
      workingDir: '$(System.DefaultWorkingDirectory)/src'
      command: 'install'
      verbose: true

Run the build again, check it's output

The log indicates that some packages are being fetched from Tencent registry instead of the official registry.npmjs.org

Azure has problems communicating with Tencent server, this is why the build will fail very often.

The reason why these packages are going to use Tencent server is because the URLs are written in package-lock.json file. This file is automatically modified when developers run npm install on their machines. A typical developer in China would have setup thier npm to use Tencent or Taobao source, due to the national firewall usually block official npm registry. 

npm config set registry https://mirrors.cloud.tencent.com/npm/

And when the developer submit the code, package-lock.json will have this URL inside it.

Fix


First, let all developers in the team use official npm registry.

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

Then, delete node_modules folder and package-lock.json under the project directory

Then, run npm install on the dev machine again.

Now, all packages will be updated to use the official npm registry.

Now, submit the code, Azure DevOps now runs npm install without network error!