Skip to content

npm Error: fetch failed with status code 406 when installing from gitlab repo

Summary

npm v3.10.10 unable to install package from gitlab repo over http. Error: fetch failed with status code 406

Steps to reproduce

[root@ryan src]# npm install http://git.mydomain.com/root/my-repo.git
npm ERR! fetch failed http://git.mydomain.com/root/my-repo.git
npm WARN retry will retry, error on last attempt: Error: fetch failed with status code 406

What is the current bug behavior?

npm supplies the HTTP request header 'Accept': 'application/x-tar, application/vnd.github+json; q=0.1' nginx returns HTTP 406

Solution

Modify nginx config to switch on the accept request header and optionally return the .tar.gz instead.

map $http_accept $tgz {
  default 0;
  '~*.*application/x-tar.*' 1;
  '~*.*application/vnd.github+json.*' 1;
}
  location / {
    if ($tgz = 1) {
      rewrite ^(.*)\.git$ $1/repository/archive.tar.gz?ref=master redirect;
    }

    proxy_cache off;
    proxy_pass  http://gitlab-workhorse;
  }
Edited by Ryan Williams