Symptom
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.
Workarounds
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
:
npm i
Then, push your application to Cloud again.
More details:
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.
-
-
Create a
.npmrc
profile and set the NPM registryIf 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:registry=<registry-url> @<namespace>:registry=<registry-url>
Notes:
-
If your project contains multiple Node.js sub-modules, you should maintain one
.npmrc
file for each module. -
Please ensure that
.npmrc
is not included in the.cfignore
-
-
Configure Cloud Foundry
manifest.json
fileSet it with the environment parameter
npm_config_registry
.For example:
--- 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!
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.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:
-
Public: https://registry.npmjs.org
-
Taobao: https://registry.npmmirror.com
- Huawei: https://mirrors.huaweicloud.com/repository/npm/