Streamlining Package Management: Configuring a Private Nexus Repository and NPM Public Registry in a Single JS Project

Pandula Dananjaya Mandawala
6 min readMay 27, 2023

In today’s interconnected development landscape, collaborating with different organizations often requires the development of specialized packages tailored to specific organizational requirements. However, leveraging the vast ecosystem of public Node modules available through the NPM public registry is equally essential.

This article delves into the intricate process of configuring and integrating a private Nexus repository with the NPM public registry, enabling developers to strike a harmonious balance between organizational customization and leveraging public dependencies. By following this comprehensive guide, developers will gain insights into seamlessly managing packages, ensuring the stability and security of their projects while maintaining the agility offered by public NPM modules.

Introduction to NEXUS Repository

What is NEXUS?

Nexus Repository simplifies the management of software artefacts by providing a centralized, secure, and customizable repository solution. It empowers organizations to efficiently organize, share, and distribute packages, enabling seamless collaboration and enhancing the overall development process.

Organizations can establish an internal package management system tailored to their specific needs by hosting a private Nexus repository. This ensures efficient collaboration, version control, and access control over proprietary code and artefacts.

There 3 versions of repositories

1. Proxy Repository

2. Hosted Repository

3. Grouped Repository

Proxy Repository

  • The proxy repository that is linked to a remote repository (ex. Npm)
  • Any request for a component is verified against the local content of the proxy repository. The request is forwarded to the remote repository if no local component is found.
  • The component is then retrieved and stored locally in the repository manager, which acts as a cache. Subsequent requests for the same component are then fulfilled from the local storage, therefore eliminating the network bandwidth and time overhead of retrieving the component from the remote repository again.

Refer to the next image for a better understanding.

(n.d.). Repository Types. Help.Sonatype.com. https://help.sonatype.com/repomanager3/nexus-repository-administration/repository-management

Developer 3 is accessing the remote repository for the package via Nexues proxy repo. Once it is cached locally, developer 2 can fetch this package through the proxy repo without accessing the remote repo.

Hosted Repository

  • This is the one that you hosted on your server through third-party software.
  • It hosts any private libs of your organization that are not public due to security vulnerabilities.
Developer 2 is hosting a package

Group Repository

  • This is a combination of proxy and hosted repositories.
  • Using this, we can have a single URL for the Proxy and Hosted Repository.

Configuring the Nexus repository for streamlining the packages

For development purposes, let’s start a local Nexus repository

Step 01

Download Nexus repository manager here: https://help.sonatype.com/repomanager3/product-information/download

Step 02

  • Open a terminal or command prompt.
  • Navigate to the extracted Nexus directory Extract and go to the bellow path [this will might be changed over time. I’m using Windows installation here.] (<nexus_home>).
  • Run the following command to start Nexus:
  • On Linux/macOS: ./bin/nexus start
  • On Windows: nexus.exe /run

Upon successful completion, the expected outcome manifests as follows.

The Nexus Repository Manager is now accessible on port 8081

Step 03

Sign in to the manager. Here you will be instructed where the password is located on the local machine. And Follow those steps.

Step 04

Move to the server administration and configurations tab, then click Repositories.

Then create an npm(proxy) repository and fill pieces of information that they request.

Here set the remote storage URL as https://registry.npmjs.org

In the same way create hosted and group repositories

Group repo creation

As previously discussed, configuring a proxy repository for public access and hosting a repository for private packages present a challenge when attempting to connect them within the same project. However, a viable solution to address this issue is to utilize a group repository. By leveraging a group repository, we can effectively consolidate and unify the public and private repositories, enabling seamless access and management within a single project. This approach streamlines the package retrieval process and ensures a cohesive and efficient development environment

Grouping Proxy and Hosted Repo

Now you have created an npm proxy repository on nexus

Step 05

Now open your private project and Create a file named .npmrc in the root level of your project.

.npmrc file in the root level in your project

Now config the nexus hosted repository URL as follows you can copy it from Nexus package manger. Before moving into this section you have to convert your username and password to base64

registry=http://localhost:8081/repository/test-repo-hosted/
_auth="<userCode>:<passCode>"

remove userCode:passCode and apply your ones

Base64 converter: https://www.base64encode.org/

Add this configuration to .npmrc file

Now add this block to your package.json file

  "publishConfig": {
"registry": "http://localhost:8081/repository/test-repo-hosted/"
}

Then run npm publishBoom! Now your private package is successfully published to the nexus hosted repository

test-nexus-repo@1.0.0 in nexus hosted repository

Let us delve into the intricacies of effectively managing this private package alongside its public packages.

Step 06

Now open your project and Create a file named .npmrc in the root level of your project same as above. The only difference is here we add grouped repository URL

registry=http://localhost:8081/repository/test-repo-grouped/
_auth="<userCode>:<passCode>"

Now just install the private package that you have published to the nexus hosted repository
Example in my case:

npm install test-nexus-repo@1.0.0

Congratulations! You have successfully completed the installation of the private packages into your project. With all the necessary dependencies in place, your project is now equipped with the specialized functionality and enhancements provided by these custom packages. Enjoy the benefits and harness the full potential of your project with these carefully tailored components at your disposal.

The private package is successfully fetched

You can now observe that your public NPM packages have been successfully cached in the proxy repository.

Cached public packages

As a result, subsequent requests for these packages will be fulfilled from the cache, resulting in enhanced performance and reduced network overhead.

This caching functionality ensures that your project’s dependencies are readily available, promoting a smoother and faster development experience.

In conclusion, configuring a private Nexus repository and integrating it with the NPM public registry opens up a world of possibilities for seamless package management. By striking the right balance between organizational customization and leveraging public modules, developers can harness the power of both worlds. Empower your projects with enhanced security, control, and accessibility, while tapping into the vast ecosystem of open-source resources.

Explore this guidence to elevate your development process and unlock new opportunities for innovation. If there is anything needs to update do not hesitate to mention it in the comment section.

Happy Coding 💃 🕺

--

--