Introduction
In this blog we will use nexus setup for NPM for a project which uses Ruby on rails. In this ruby on rails project we are using YARN and BUN framework. We did setup a nexus NPM proxy and private repo but yarn could not fetch the npm dependencies via the nexus proxy. The rails assets precompile task however uses bun which uses npm. YARN is a wrapper on the NPM hence it does not entirely rely on NPM. BUN is a runtime which performs all the NPM tasks. Our goal in this blog is to understand how to handle this scenario in a ruby on rails project when use NEXUS NPM proxy.
Technical specifications
We have below versions of the tools
- NEXUS : 3.37.1-01
- NPM : 9.5.0
- NODE : v18.15.0
- NVM : 0.39.1 ( Node version manager )
- Mac OS : Sonoma 14.2
- YARN : 3.5.1
- BUN : 1.0.0
Steps
Below are the steps required
- Configure NPM proxy in NEXUS
- Configure NPM private hosted repo
- Configure NPM group in NEXUS
- Change local NPM config for registry
- Change the YARN config for registry
- Configure BUN configuration for registry
Configure NPM proxy in NEXUS
It is a standard proxy addition in the nexus repository settings tab. Click Create Repository button on the top left corner of the table. In this path below fill the in the details on the UI form
Login -> Settings -> Repositories -> Create repository -> npm (proxy)
FORM Values
- Name : Enter some name for your proxy repo ( Ex: npm-public )
- Remote Storage : The value expected here is https://registry.npmjs.org
- Blob store : Select a blob store you have for your setup. In our case we are having s3 storage
Below are the screenshots showing the mandatory fields mentioned above. You can keep the other fields as default values for now
 |
Create npm proxy - page 1 |
 |
Create npm proxy - page 2 |
 |
Create npm proxy - page 3 |
Configure NPM private hosted repo
NPM private hosted repo is internal to your organisation. It can be used for publishing node modules and libraries to be used within the organisation. The process to create it is similar to NPM hosted proxy. Only difference is that it does not have a remote server url in case of hosted private repo. Goto below flow path
Login -> Settings -> Repositories -> Create repository -> npm (hosted)
FORM Values
- Name : Enter some name for your proxy repo ( Ex: npm-private )
- Blob store : Select a blob store you have for your setup. In our case we are having s3 storage
Please refer the screenshots below :
 |
NPM private hosted repo - Page 1 |
 |
NPM private hosted repo - Page 2
|
Create NPM group in NEXUS
Now that we have the npm-public and npm-private repos. We don't want to use two links for accessing them or managing the access control for the users within the organisation. Hence we will create a group of these two repos called a repositories group. In the NEXUS to create group follow below flow path :
Login -> Settings -> Repositories -> Create repository -> npm (group)
FORM Values
- Name : Enter some name for your proxy repo ( Ex: npm-private )
- Blob store : Select a blob store you have for your setup. In our case we are having s3 storage
Once the form values are filled, select the member repositories i.e. npm-private and npm-public. Refer the screenshot below.
 |
Create NPM group |
Change the local NPM configuration for registry
You local NPM configuration by default will go to NPM registry directly. We will now change it to use the NEXUS npm group so that it first checks if the artefact is available in your internal blob store. If yes then download it from there else fetch it from remote npm registry and store in the blob store for future reuse. The group also handles the scenario that if an artefact is present in the npm-private then fetch it from there else search as per the previous flow. In nutshell we have to configure the npm group url in the npm configuration on the local.
There are two types of configuration
- global : user scoped config
- local : project scoped config
To view the configuration resolution run below command. It will show the global config and out of the global config which parameters are overridden by project specific config.
NOTE : The below command should be run from the root folder of your project which contains the .npmrc. If the project does not contain one then it will not show the project scoped values.
npm config list
The output will contain some kind of format below
; "user" config from <path of the .npmrc file for user>
<registries configured in that file>
; "project" config from <path of your current project .npmrc>
<registries and other params overridden at project npmrc file>Create a .npmrc file in the project root folder and below content into that.
registry=<complete url path of nexus npm-group>
//<neuxs npm group url without http: >:_auth="<base64 encoded value of username and password>"- Nexus group url : You can get from the repositories list table. There is a column named as URL which contains the copy icon. While copying the url do not copy the http: value we only want the part after the colon.
- Base 64 encoded username and password : If you are using MAC or LINUX use the command mentioned below for generating this value
Base64 encoded creds command
echo -n 'username:password' | base64
Now when you try to do NPM login then you will be prompted with username and password. But you will see they are pre-populated. You need to only confirm. The password will be masked though. Just type enter and proceed. You will now be authenticated using the credentials added to the _auth.
Change the YARN config for registry
Now the NPM configuration is ready we need to change the yarn configuration so that it points to the NEXUS registry. Although I did not find any benefit changing this as the rails assets precompile uses bun for that hence I am documenting this step for consistency. Follow below steps.
Check existing yarn configuration using below command and press enter. It will show the existing yarn configuration as key value pairs.
yarn config
In the output check for the value of the field npmRegistryServer. We will now change it to our nexus npm-public repository. Use below command to change the yarn config npm registry value.
yarn config set npmRegistryServer <complete url path of nexus npm-public repository>Once the value is set verify the same using yarn config command mentioned above.
Change the BUN configuration for the registry
BUN configuration should be added in the file called bunfig.toml. Create a file in the root folder of the project with this name and add below details to it.
[install]
# set default registry as a string
registry = "https://<nexus user>:<nexus password>@<nexus hostname>/repository/npm-public/"
[install.scopes]
"@mynpm-public" = "https://<nexus user>:<nexus password>@<nexus hostname>/repository/npm-public/"All set now to use the assets precompile command. Run below command and see now the dependencies are fetched via nexus and cached in nexus.
rails assets:precompileSummary
In this blog we learnt about how to point my ruby on rails project to my nexus repositories for NPM.
Comments
Post a Comment