Installing NPM packages from GitLab repositories

15th February, 2015 — Aral Balkan

TL; DR: If you want to install an NPM package from a GitLab repository, use the git+https (or git+http) protocol, not git:// or https://.

This one always catches me out. If you want to specify that an NPM package should be installed from a GitLab repository (e.g., from source.small-tech.org), you need to use the git+https protocol (or HTTP if you’re being naughty and running GitLab without TLS).

package.json{
  …
  "dependencies": {
    "indie-set": "git+https://source.small-tech.org/project/set.git",
  …
  }
}

This catches me out as git:// URLs work for GitHub repositories. So, for example, the following is perfectly valid and works:

package.json{
  …
  "dependencies": {
    "ws": "git://github.com/websockets/ws.git",
  …
  }
}

If you try that with GitLab, however, you’ll get an error message similar to:

npm ERR! git clone git://source.small-tech.org/project/set.git Cloning into bare repository '/Users/aral/.npm/_git-remotes/git-source-ind-ie-project-set-git-445a3a05'...
npm ERR! git clone git://source.small-tech.org/project/set.git fatal: unable to connect to source.small-tech.org:
npm ERR! git clone git://source.small-tech.org/project/set.git source.small-tech.org[0: 37.139.13.136]: errno=Connection refused
npm ERR! Darwin 14.1.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v0.12.0
npm ERR! npm  v2.5.1
npm ERR! code 128

npm ERR! Command failed: git clone --template=/Users/aral/.npm/_git-remotes/_templates --mirror git://source.small-tech.org/project/set.git /Users/aral/.npm/_git-remotes/git-source-ind-ie-project-set-git-445a3a05
npm ERR! Cloning into bare repository '/Users/aral/.npm/_git-remotes/git-source-ind-ie-project-set-git-445a3a05'...
npm ERR! fatal: unable to connect to source.small-tech.org:
npm ERR! source.small-tech.org[0: 37.139.13.136]: errno=Connection refused

And if you try to install it using just the https protocol, you’ll get an error similar to:

npm WARN package.json labs@0.0.1 No repository field.
npm ERR! fetch failed https://source.small-tech.org/project/set.git
npm WARN retry will retry, error on last attempt: Error: fetch failed with status code 406