This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

This article describes how to use Verdaccio to build a private NPM repository, and how to map to a local directory when using Docker, making it easy to do various operations on the repository. The system environment is Linux.

verdaccio

Verdaccio is an open source, lightweight, private NPM Proxy Registry (NPM proxy repository) that is easy to install and use. Than from [email protected].

The installation

usenpm / yarnGlobal Installation

  • Node.js v12+

  • NPM 4 x + or yarn

  • Global installation

    npm install -g verdaccio
    
    yarn global add verdaccio
    Copy the code
  • Start the

    Verdaccio or use pm2 start verdaccioCopy the code

    Use a browser to access the Web service, port number 4873, http://0.0.0.0:4873, the access is successful.

usedockerMirror installation

  • Install docker

  • Install the Verdaccio image under Docker

    Run the following command:

    docker pull verdaccio/verdaccio
    Copy the code

    The image is successfully installed:

  • Run verdaccio, run the image using the docker command, and create the Verdaccio container

    Docker runit --name verdaccio -p 4873:4873 verdaccio/verdaccioCopy the code

    Container started successfully:

    Once successfully launched, you can actually access it in your browser, http://0.0.0.0:4873. Here are a few docker commands that manipulate containers.

  • Docker is part of the command that operates on the container

    Docker start CONTAINER_ID Check the CONTAINER: Docker CONTAINER ls (docker ps) Stop CONTAINER: Docker stop CONTAINER_ID Delete a container: docker rm CONTAINER_ID

  • Mapping a Local Directory

    First, create the /home/verdaccio directory, where you will do the following. Next, create the conf directory and add the config.yaml configuration file for Verdaccio. Then the mapping directory is processed in the following two ways:

    1. Map the local directory directly and start Verdaccio

      docker run -it --name verdaccio -p 4873:4873 -v /home/verdaccio/storage:/verdaccio/storage -v /home/verdaccio/conf:/verdaccio/conf -v /home/verdaccio/plugins:/verdaccio/plugins verdaccio/verdaccio
      Copy the code

      -v: indicates that a container directory is mapped to a local directory

    2. Yml/docker-compose up/docker-compose up/docker-compose up

      version: '3'
      services:
        verdaccio:
          image: verdaccio/verdaccio
          container_name: "verdaccio"
          network--mode: "bridge"
          environment:
            - VERDACCIO_PORT=4873
          ports:
            - "4873:4873"
          volumes:
            - "/home/verdaccio/storage:/verdaccio/storage"
            - "/home/verdaccio/conf:/verdaccio/conf"
            - "/home/verdaccio/plugins:/verdaccio/plugins"  
          network_mode: "bridge"
      Copy the code

    Note: The local directory /home/verdaccio/storage needs to be set permission, otherwise the operation will fail due to permission problems.

    chown -R 10001:65533 /home/verdaccio/storage
    Copy the code

    After the local directory mapping is successful, many operations will be simplified. For example, a storage directory will be created in the /home/verdaccio directory. If a package is published, the corresponding package folder can be found in the data directory, and all uploaded NPM packages can be viewed. Conf will also be mapped to the configuration file, so that we can modify the configuration information if necessary.

  • Copy the Verdaccio configuration file

    If you do not want to do local directory mapping, you can also use the following command to copy the configuration file of the Verdaccio image. Docker cp: Copy files or folders between containers and local file systems.

    docker cp verdaccio:/verdaccio/conf/config.yaml /home
    Copy the code

The private library is successfully deployed

After verdaccio is installed and started successfully, it can be accessed directly from the network. When the page shows the following figure, it indicates that the private library has been set up successfully, and the private package can be released and downloaded later.

Release a package

Once you have a private library, you can publish NPM packages on it. However, you need to add a user first, set the user name and password, and then directly send packets.

  • Add user

    NPM adduser - registry at http://0.0.0.0:4873/Copy the code

    Enter the user name, password, and email address.

  • publish

    Publish directly when you need to publish an item to a private library.

    NPM publish - registry at http://0.0.0.0:4873/Copy the code

    After successful publishing, refresh the page to see the newly released package.

install

Add the.npmrc file to the project directory to specify the warehouse address.

Registry = http://0.0.0.0:4873/Copy the code

Use the NPM install package name to install private packages.

Verdaccio configuration file interpretation

Common configurations are as follows:

  # Save path of prime package
  storage: /verdaccio/storage/data
  The save path of the plugin
  plugins: /verdaccio/plugins

  # Access through the Web
  web:
    title: Verdaccio

  The password file does not exist initially
  auth:
    htpasswd:
      file: /verdaccio/storage/htpasswd
      # max_users: 1000
      The number of registered users is 1000 by default. If the value is -1, NPM adduser cannot be used to register users. In this case, you can directly modify file to add users.

  If local does not exist, read the address of the warehouse
  uplinks:
    npmjs:
      url: https://registry.npmjs.org

  Package access permissions can be matched to a specific project or wildcard
  # access download; The publish; Unpublish cancel publish;
  [uplinks] # proxy = 'unplinks

  # $all indicates that anyone can perform this operation
  # $authenticated is available for registered accounts
  # $anonymous This operation can be performed by anonymous users
  You can also explicitly specify users in the htpasswd user table, which can be configured as one or more users.
  packages:
    '@ * / *':
      access: $all
      publish: $authenticated
      unpublish: $authenticated
      proxy: npmjs
    
    '* *':
      access: $all
      publish: $authenticated
      unpublish: $authenticated
      proxy: npmjs

  # server specific
  sever:
    keepAliveTimeout: 60

  middlewares:
    audit:
      enabled: true

  # Log setting
  logs: { type: stdout.format: pretty.level: http }
Copy the code