Home About Me

When npm Is Blocked: Mirrors, Proxies, and Manual Installs

When the Great Firewall decides to block npm, the problem is still a proxy problem at heart. There are a few ways around it: use a domestic registry mirror, send npm through a local proxy, or simply download the package and put it in place.

Use a domestic npm mirror

The first option is a China-based npm mirror at http://cnpmjs.org. Install cnpm from a command prompt and go grab a meal; if it still hasn't finished, switch to a direct registry target:

npm install -g cnpm --registry=http://r.cnpmjs.org

The --registry option tells npm which repository to download from. cnpm mirrors the official npm registry, so package updates on npm are reflected there too.

You can also set the registry once and skip the flag next time:

npm config set registry="http://r.cnpmjs.org"

With that in place, npm points to the domestic repository by default.

Once cnpm is installed, you can use it directly:

cnpm install -g package_name

The idea is the same as pointing npm at the mirror.

Send npm through a proxy

If you have goagent or another local proxy, npm can use it. A full configuration might look like this:

# Global path, i.e. for npm install -g; this is what -g refers to
npm config set prefix="c:\nodejs"

# Usually goagent is used for proxying, and its default port is 8087
npm config set proxy=http://127.0.0.1:8087

# Set the HTTPS proxy
npm config set https_proxy=http://127.0.0.1:8087

# Remember to set this; otherwise you may end up using a proxy but downloading from a domestic registry
npm config set registry=http://registry.npmjs.org

After configuring, make sure the proxy is running. If goagent is closed, npm will throw errors. In practice, though, most of those lines aren't necessary. This is usually enough:

npm config set proxy=http://127.0.0.1:8087

Why? Global installs don't require a custom prefix, the registry already defaults to http://registry.npmjs.org, and leaving https_proxy unset still works. That leaves the single proxy setting.

Download packages manually

If even that feels like too much, download the package files directly and place them inside node_modules. For a global module, find the global node_modules directory, extract the package there, and it is effectively installed.