Workarounds When npmjs.org is Not Accessible
I’m writing this post since many developers and service owners especially in the globe haven’t had this problem before and see it as a blocking issue. So, I will show you some workarounds to solve the problem of installing node packages.
When locally installing packages for your projects or deploying your applications to Cloud Foundry runtime, you hit errors like timeout (e.g. shown as below), and you tried to ping the registry server but failed.
-----> Building dependencies Prebuild detected (node_modules already exists) Rebuilding any native modules rebuilt dependencies successfully Installing any new modules (package.json) Error staging application sbf-sample: timed out after 15 minute(s)
By default, it will install the node packages from , which is the most commonly used public NPM registry. When it is not accessible for any reason, we can take some actions to solve it.
Workaround 1: Make Project Self-contained
When deploying your application to a Cloud environment like Cloud Foundry runtime, you found the network is not stable when installing packages from the container. You can try to make your project self-contained, which means you should pre-download all the dependencies and push them together with your code to Cloud.
Under the same directory as the file
package.json, execute the following command to install all dependencies into the
Then, push your application to Cloud again.
Workaround 2: Switch NPM Registry
Of course, you can directly switch NPM registry to solve the problem. You can configure it in the following ways:
Execute NPM command:
npm config set registry <registry-url>
This solution has a few drawbacks:
You have to recall and re-type the correct registry URL every time you change your registry server.
It ignores the fact that there are multiple configuration options that differ between registry servers, e.g. username and password. You must change these options too when switching to another registry.
You doesn’t have the chance to set it when deploying it to Cloud environment like Cloud Foundry runtime. For this case, you can use the next method to configure it.
.npmrcprofile and set the NPM registry
If you hit the problem when deploying your applications to Cloud environment like Cloud Foundry runtime. You can try this workaround.
.npmrcfile under the root directory of your project / sub-modules with the following content:
Cloud Foundry runtime would identify it automatically and try to install packages from the registry you set.
If your project contains multiple Node.js sub-modules, you should maintain one
.npmrcfile for each module.
Please ensure that
.npmrcis not included in the
.cfignorefile before pushing your application to Cloud.
Configure Cloud Foundry
Set it with the environment parameter
--- applications: - name: broker-tia memory: 128M env: npm_config_registry: <registry-url>
Use npmrc Tool
If you use a private npm registry, you know the pain of switching between a bunch of different .npmrc files and manually managing symlinks. Let that be a problem no more!
npmrcis here to save the day, by making it dead simple to switch out your .npmrc with a specific named version. It also tries to protect you from your own stupid self by making sure you don’t accidentally overwrite a .npmrc that you actually want to keep.
For more details on the tool, please read: https://www.npmjs.com/package/npmrc
You can choose your registry according to your preference and purpose. For example:
- Huawei: https://mirrors.huaweicloud.com/repository/npm/