Private Npm repository setup

What is a private Npm repository?

Private Npm repositories are private Npm package management repositories for in-house developers, as well as private Git repositories

Why a private Npm repository?

Packages developed in-house may not be open source and shared, but are intended for internal use, so they cannot be uploaded to the Npm website

The Npm website is also private, but for a fee.

At this time, the local private Npm repository solves this problem. The local private repository has the characteristics of free, local, private and high-speed download.

Because it is deployed locally, downloading packages is very fast.

verdaccio

The installation

npm install verdaccio -g
Copy the code

Start the

Yaml # Manually specify port verdaccio -l 0.0.0.0:9000Copy the code

If the following information is displayed, the startup is successful

warn --- config file - /home/root/.config/verdaccio/config.yaml warn --- http address - http://localhost:4873/ - Verdaccio / 4.10.0Copy the code

After startup, a configuration file config.yaml is generated in the home directory of the startup user

For example: / home/root/config/verdaccio/config. Yaml

Main configuration file

  • When config.yaml is started, it generates the repository configuration file, in which the repository configuration is written
  • Htpasswd User account information file generated after a user is registered, which records the account, password, and creation date
  • Storage All uploaded packages are saved here

Configuration items

Note the indentation in the configuration file before configuration; otherwise, an error will be reported

Opening the config.yaml configuration file, we can see the following items

  • Storage Sets the directory for storing the uploaded package
  • The plugins directory
  • This section describes how to configure the front-end web page and set the page title and image
  • I18n Sets the language of the page
    • Web Indicates the default web language zh-CN
  • Auth Sets the content related to the account
    • Htpasswd Specifies the account password
      • File File directory where the account and password are stored
      • Max_user Indicates the maximum number of registered users. -1 indicates that registration is prohibited but can be manually added in the account file
  • Uplinks sets upstream matching, which is mainly used when packets cannot be matched. Where can the system find packets
  • Packages Configures packages for controlling the upload, download, and access permissions of packages
    • Access Indicates whether the file can be downloaded
    • The publish rights
    • Unpublish Cancels the publishing right
    • There are roughly three kinds of control over permissions
      • $all All logged-in and unlogged-in users can access it
      • $authenticated is only accessible to logins
      • User A, b, and V are specified. Only this user can access the system

A simple configuration is provided below

# set the save directory
storage: ./storage
plugins: ./plugins
web:
  title: Npm
# Set Chinese display
i18n:
  web: zh-CN
# Set two upstream streams
uplinks:
  npmjs:
    url: https://registry.npmjs.org/
    agent_options:
      keepAlive: true
      maxSockets: 40
      maxFreeSockets: 10
  cnpmjs:
    url: http://r.cnpmjs.org/
    agent_options:
      keepAlive: true
      maxSockets: 40
      maxFreeSockets: 10
# Add a permission control for @my/* package, only allow login users to download and upload
packages:
  '@my/*':
    access: $authenticated
    publish: $authenticated
    unpublish: $authenticated

# wildcard item, that is, packets that cannot be configured above go through this item
  '* *':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    If you can't find this package locally, you can set multiple packages to find it in order
    proxy: cnpmjs npmjs
Set the listening port and support IP access
listen:
  0.0. 0. 0: 3000
Copy the code

Registered account

npm adduser --registry http://localhost:4873/
Copy the code

After the execution will ask you to enter the account, password, email must be remembered

Use in development

NRM Repository source management tool

NRM makes it very easy to switch back and forth between different sources

The installation

npm install -g nrm
Copy the code

Add repository source

NRM add [name] [url] # NRM add mynpm http://127.0.0.1:3000Copy the code

Using warehouse sources

NRM ls // Lists the warehouse source NRM use [name] // Selects the warehouse sourceCopy the code

Publish or cancel publish the NPM package

When publishing an NPM package, make sure NRM ls checks to see if the private repository source is selected, otherwise it will be published to another NPM repository (if logged in)

npm publish
npm unpublish
Copy the code

Download the private NPM package

NPM install XXX --registry http://127.0.0.1:3000 # or NRM use private source NPM install XXXCopy the code

** Note: ** If you use NPM install to download a non-local package after switching to a private source, the proxy upstream will come into play and the system will go upstream to select the package until the last proxy.