Why build an internal NPM service?

Nowadays, the front-end circle is very prosperous, and the technology is generally separated from the front and back ends, which leads to the increasing division of labor pressure on the front end. Front-end projects tend to become complex and complicated, and they are divided into multiple projects according to different business lines. At this time, the common component library plays an important role. Uploading to the NPM library is a good option, different projects point to one NPM source, and then NPM install is convenient and fast.

The Nexus profile

Nexus is the Maven repository manager. If you use Maven, you can download artifacts from the Maven central repository, but this is usually not a good idea. You should set up a Maven repository server locally and maintain the local repository while proxy the remote repository to save bandwidth and time. The Nexus meets that need. In addition, it provides powerful warehouse management capabilities, component search capabilities, and it is reST-based. The user-friendly UI is an EXTJS REST client that uses less memory and is based on a simple file system rather than a database. These advantages make it increasingly the most popular Maven repository manager.

Nexus Private server is a special remote repository on a local area network (LAN) designed to proxy remote repositories and deploy third party components. With private servers, when Maven needs to download a JAR, it requests the private server first and downloads it to the local repository if it exists on the private server. Otherwise, the private server directly requests the external remote repository to download the JAR package to the private server and provide it to the local repository for download.

run

windows

  1. Set nexu.exe to run as an administrator
  2. CD {nexus unpacked directory /nexus- version /bin/}
  3. nexus.exe /install

  4. nexus.exe /start

Mac

  1. CD {nexus unpacked directory /nexus- version /bin/}
  2. ./nexus run

Type http://localhost:8081 into your browser, and if all goes well, you’ll see the Nexus Repository Manager administration page.

The default account is admin and the password is admin123. If Incorrect username or password is reported, or no permission to use the application. It into/sonatype – work/nexus/admin. The password to see the password

Type is introduced

  • proxy

    Proxy repository, a proxy cache of resources from the common NPM server, reduces repeated downloads and speeds up downloads for developers and CI servers.

    The warehouse address is:http://localhost:8081/repository/npm-center/
  • group

    Repository groups, used to combine multiple Hosted/Proxy repositories, do not need multiple references when your project wants to use resources in multiple Repositories, just a group.

    The warehouse address is:http://localhost:8081/repository/npm-public/
  • hosted

    Is a local repository for uploading your own NPM packages as well as third-party NPM packages.

    The warehouse address is:http://localhost:8081/repository/npm-release/

Configure the NPM private server

  • Initialize the NPM project

Go to any directory and initialize the package using NPM init -y

  • View the default repository address for NodeJS

Registry.cnpmjs.org/ is displayed if not

npm config get registry
Copy the code
  • Next, reset the default nodeJS repository address (global NPM private server).

npm set registry http://localhost:8081/repository/npm-public/
Copy the code
  • Log in to NPM private server

Input user name/password/mailbox in turn, user name/password/mailbox are configured on the private server. –always-auth Sets permanent login

  1. Log on to the group
npm login --registry=http://localhost:8081/repository/npm-pulbic/ --always-auth
Copy the code
  1. Login hosted
npm login --registry=http://localhost:8081/repository/npm-release/ --always-auth
Copy the code

After the above Settings, open the ~/.npmrc file verification, the content is roughly as follows:

If Unable to authenticate is encountered during operation, need: BASIC Realm =”Sonatype Nexus Repository Manager”, go to “Sonatype Nexus Repository Manager”, set — Security — Realms, Add NPM Bearer Token realms to Active and save.

  • Publish a package

  1. In the package. The json configuration
"publishConfig" : {
    "registry" : "http://localhost:8081/repository/npm-release/"
}
Copy the code

NPM publish at the root of the package.

  1. If you don’t want to configure it in package.json, you can specify it on the command line.
npm publish --registry=http://localhost:8081/repository/npm-release/
Copy the code

  • The installation package

NPM install vue-test NPM install vue-test NPM install vue-test

As for the package name conflict problem, I found that if the package name is the same, if the version number is different, it will be merged into the original, but if the version number is the same, the command line shows that the release is successful, but there is no change on the private server, the reason is unknown.

  • Update the NPM package version

If the contents of the NPM package are updated, you need to upgrade the NPM package version. We use semantic versions.

Format: Major version number. Minor version number. Revision number Increment rule: Major version number: when you make an incompatible API change Minor version number: when you make a backward-compatible functional addition Revision number: when you make a backward-compatible problem fix Prior version number and build information can be added to the major version number. Second version number. Revision number "is followed as an extensionCopy the code

Use NPM version to view the current package version number

Update the version number and release it

  1. throughNPM Version Indicates the new versionDirect update version

  1. throughnpm version <update_type>Automatic version change

Update_type value, meaning and demonstration:

  1. Patch: patch, example of version change:V1.0.1 – > v1.0.2, run the following command:npm version patch
  2. Minor: tweaking, example of version change:V1.1.0 – > v1.2.0, run the following command:npm version minor
  3. Major: big change, example of version change:V1.0.0 – > v2.0.0, run the following command:npm version major

Here, for example, with Minor, it’s time for a miracle:

  • Delete package (with pit)

Cancel publishing a single version of the package

npm unpublish [<@scope>/]<pkg>@<version>
Copy the code

Cancel publishing the entire package

npm unpublish [<@scope>/]<pkg> --force
Copy the code

But! Error: NPM unpublish [email protected]

Add the specified link to indicate that the deletion succeeded.

But again! Private server not deleted successfully! I chose to abandon the command line, log in to the private server, and delete with one click. Of course, we do not recommend this method, the new bag is fine, the old bag is dangerous, in case someone uses this bag. If you really don’t want to maintain the package anymore, we can also deprecate the package. We recommend deprecating instead of deleting it.

NPM – deprecated

npm deprecate <pkg>[@<version range>] <message>
Copy the code

If your goal is to encourage users to upgrade, or if you no longer want to maintain packages, consider using the Deprecate command instead. This command updates the package’s NPM registry key, providing deprecation warnings to all who try to install it.

Deprecate the specified version

NPM deprecate my-thing@"< 0.2.3" "Critical bug fixed in V0.2.3"Copy the code

SemVer passes an interpretation of the scope of this directive so that they do not include pre-release versions. Such as:

npm deprecate [email protected] "1.x is no longer supported"
Copy the code

reference

  • Using Nexus3 Repository Manager builds NPM private servers: www.cnblogs.com/cangqinglan…
  • NPM nexus of E401 E500 error handling records: blog.csdn.net/lqh4188/art…
  • Semantic version 2.0.0: semver.org/lang/zh-CN/
  • NPM – unpublish: docs.npmjs.com/cli/v7/comm…
  • Npm-deprecate:docs.npmjs.com/cli/v7/comm…

Download address

  • Download the latest version of Nexus at www.sonatype.com/nexus-repos…
  • Download failed because the website regularly, so I download coexist in other place to baidu cloud disk, Nexus3 download address for: link: pan.baidu.com/s/1Pfteu9NU… Password: 5 c4m