3

Workarounds When npmjs.org is Not Accessible

 1 year ago
source link: https://blogs.sap.com/2022/11/04/workarounds-when-npmjs.org-is-not-accessible/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
November 4, 2022 2 minute read

Workarounds When npmjs.org is Not Accessible

0 1 103

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.

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 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.

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

And, under the same directory as the file package.json, execute the following command to lock your dependencies:

npm shrinkwrap

A npm-shrinkwrap.json file will be generated and 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.

  • 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:

    registry=<registry-url>
    @<namespace>:registry=<registry-url>

    Cloud Foundry runtime would identify it automatically and try to install packages from the registry you set.

    Note: If your project contains multiple Node.js sub-modules, you should maintain one .npmrc file for each module.

  • Configure Cloud Foundry manifest.json file

    Set 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:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK