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 https://registry.npmjs.org, 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 node_modules:
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.
Create a .npmrc profile 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.
Create a .npmrc file under the root directory of your project / sub-modules with the following content:
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! npmrc is 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.