4

Solving "npm install" ECONNRESET Error on Azure DevOps

 1 year ago
source link: https://edi.wang/post/2023/8/16/npm-econnreset-azure-devops
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

img-5c2f53b9-9306-495b-beb4-2dd4998edae5.png

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

img-29179f16-8207-4c66-b91e-af755c7246df.png

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.


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.

img-70a6638c-7c62-425e-9537-5b2a35a72450.png

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

img-8584a7d9-9e75-4cba-b59d-43af932cd5a8.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK