This is the first day of my participation in the August More Text challenge

Demand background

Usually in the project work may use a lot of universal code, such as framework class, tool class and common business logic code, by packaging and published to the NPM central warehouse or private warehouse, to maintain and manage the code, convenient use of common code. Components of code that are not easily exposed can be published to a private repository.

Advantages of a private NPM component library:

  1. Private NPM packages are only available to the company’s internal LAN
  2. It is faster than downloading packages directly from NPM and faster than using Taobao images.
  3. Permission management can be configured for publishing and downloading NPM packages

Verdaccio installation and deployment

Verdaccio is a lightweight NPM package management tool, which can host private modules and has a caching function. When the corresponding modules cannot be found in the private repository, it will be downloaded from the NPM public server and cached.

Verdaccio relies on nodeJS and THE NPM environment. Materials prepared are as follows:

  1. nodejs
  2. npm
  3. verdaccio
  4. A server

1. Install NodeJS and NPM on the server (Linux as an example)

PS: You can also download the Linux installation package from the official website and upload it to the server to decompress the installation package

# download wget https://nodejs.org/dist/latest/node-v16.4.0-linux-x64.tar.xz # extract tar xf node - v16.4.0 - Linux - x64. Tar. Xz # CD node-v16.4.0-linux-x64 # Run the node command to check the version./bin/node -v # Create a soft connection between nodejs and NPM ln -s /usr/local/node-v16.4.0-linux-x64/bin/npm /usr/local/bin/ln -s /usr/local/node-v16.4.0-linux-x64/bin/node /usr/local/bin/node -v NPM -vCopy the code

2. Install Verdaccio globally

Install verdaccio NPM i-g verdaccioCopy the code

3. Configure the private library file

CD apu- NPM # Run verdaccio verdaccioCopy the code

The result is as follows:

Verdaccio doesn't need superuser privileges. Don't run it under root.

warn --- config file - /root/.config/verdaccio/config.yaml

warn --- Plugin successfully loaded: htpasswd

warn --- Plugin successfully loaded: audit

warn --- http address - http:/ / localhost: 4873 / - verdaccio / 3.10.2
Copy the code

From the console output as you can see, verdaccio configuration file path in/root/config/verdaccio/config. The yaml, default access the address http://localhost:4873/

Modify the verdaccio configuration file

# Interface related configuration Web: # WebUI is enabled by default. If you want to disable it, simply uncomment this line # enable: False title: xx- private library # user related, such as registration, authentication plug-in (default use htpasswd) auth: htpasswd: /htpasswd # Maximum number of users allowed to register, default is "+ INF ". # You can set this value to -1 to disable registration for max_users: 100 LISTEN: Uplinks: NPMJS: url uplinks: NPMJS: url https://registry.npmjs.org/ # is used to configure the distribution package, delete, view the permissions of # 3 key word: "$all"- all users, "$anonymous"- unregistered users, "$authenticated"- registered users packages: # scope packages (e.g., for the first one, if we publish package names like @jex/test it will hit) '@*/*': Access: $all publish: $authenticated unpublish: $authenticated # If no private package is available locally, the proxy requests the 'NPMJS' repository proxy: NPMJS '*': access: $all publish: $authenticated unpublish: $authenticated Proxy: NPMJS Middlewares: audit: enabled: true middlewares: audit: enabled: true # Logs: - {type: Stdout, format: pretty, level: HTTP} # notify: 'dingtalk': method: POST headers: [{' content-type ': 'application/json;charset= utF-8 '}] # https://oapi.dingtalk.com/robot/send?access_token= * * * *, # push content content: '{" color ":" green ", "message" : "new package issued: *{{ name }}*","notify":true,"message_format":"text"}'Copy the code

4. The service is started

  1. Remote login/remote control Access ali Cloud server
  2. Run the command verdaccio –config./config.yaml in the terminal window

5. Pm2 manages the Verdaccio process

Start Verdaccio with PM2 and keep the process open at all times


Install the pm2

npm install -g pm2
Copy the code

Start Verdaccio with PM2 to keep the process open at all times

pm2 start verdaccio
Copy the code

Clients publish NPM packages to private libraries

1. User registration

NPM adduser --registry http://server IP address :4873Copy the code

2. User login

NPM will automatically cache after login. Next time you do not need to login to NPM login --registry http://server IP :4873Copy the code

3. How to use the private repository NPM package

NPM config set registry http://server IP :4873Copy the code

4. Publish the NPM package to a private repository

NPM publish --registry http://server IP :4873Copy the code

Package GUI administration window

Open the browser http:// server IP address :4873 and view the NPM package of the private library