This is the second day of my participation in the August More Text Challenge

Demand background

There are many tools for doing private libraries, such as verdaccio, the lightweight NPM package management tool introduced in the previous article. However, it happens that Maven is using Nexus as a private server, so I chose Nexus as a private server for NPM, which is easy to maintain with Maven.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — the nexus installation process omit — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Create the repository

Nexus Repository Manager 3 can be used for many types of package management. Here we want to build is NPM package management private server.

Log in and click the setting button on the interface, as shown in the picture below.

Go to the Repository -> Repository -> Create Repository, which will go to the Repositorty option

NPM has three options:

  • The first is the proxy NPM repository, which proxy caches resources from the common NPM server to reduce repeated downloads and speed up downloads for developers and CI servers. Select NPM (Proxy) and enter Name and Remote Storage (public library domain Name).

  • Second: private NPM repository for uploading your own NPM packages as well as third-party NPM packages. Same creation steps, except that the selected repository type is NPM (Hosted). Enter only Name

  • The third type: NPM repository group, which is used to unify multiple internal or external NPM repositories into one NPM repository. Packages in other warehouses that have been added to the NPM repository group are accessible through the NPM repository group. Create a new NPM repository group and add the two NPM repositories just created above. This allows you to access both the public NPM repository and your own private NPM repository through the NPM repository group. Same steps as above, except select the repository type as NPM (Group), give a Name Name, and then select the other NPM repositories that you want to add to the group.

Verify availability

In Repositories, click on the Repository to be created. You can view the URL for the repository.

Create the.npmrc file in the project directory. The contents of the document are as follows:

Registry = private library package address

–loglevel info: set the loglevel to info. You can see from the printed log that the package was downloaded from the NPM private server:

$ npm --loglevel info install react

Publish to NPM private server

In addition to installing dependencies from the NPM private library, we also need to package and publish our internal company code to the private library. Here, we need to set up the Nexus Repository Manager permissions so that we can use the NPM login authentication to login to our private server.

  1. Add Permission Authentication: Set permissions, left menu Security -> Realms menu, to add NPM Bearer Token Realm to the right

  1. Create the roleCreate the nx-deploy role and assign it to nx-repository-view-– * permissions

  1. Create users: Create user deployer and set role to nX-deploy
  2. Global login: Run the command, prompting you to fill in the account password and email address. After the verification is successful, a url and token of the warehouse will be inserted into the. NPMRC file in the user’s home directory.
NPM login --registry= private library package addressCopy the code
  1. In the.npmrc file, _auth is the base64 value of username:password
Registry = private package [email protected] always-auth=true _auth="ZGVwbG95ZXI6ZGVwbG95ZXI="Copy the code
  1. Publish the control to the NPM private library: configure it in package.json and then do NPM publish in the package root directory.
"PublishConfig" : {"registry" : "private library NPM - Hosted package address"}Copy the code

If you do not want to configure it in package.json, you can also specify it on the command line as follows:

NPM publish --registry= Private library npm-Hosted package addressCopy the code

Error resolution

  1. 401 No Permission:
npm install
npm ERR! code E401
npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manager"
Copy the code

Solution: To log in to the Nexus, check Administration — Secturity — Anonymous Allow Anonymous Users to access the server.