table of Contents
Build a private npm warehouse solution based on verdaccio
1 Introduction
The front-end ecology has been very prosperous. As the needs become more and more complex, the front-end applications are getting bigger and bigger. In order to better service decoupling, teamwork and other reasons, generally large-scale front-end applications will be divided into several projects Services are maintained, and in these services, there are generally a large number of the same components, especially the components of the UI part. Obviously, we should not copy these components in each service, so if I want to add or update a component , Then it will be very painful, so most of the practice is to extract the public components. So the question is, where should I put it out? It is definitely not appropriate to put on npm. This is an internal component of the company. Therefore, the common practice is to build a private npm warehouse to realize the company's internal private library. In short, it has the following advantages:
- Host company's internal components, achieve privatization, and easy to manage and update
- Private warehouses generally use the company's internal servers, and download speeds are leveraged
2. Selection of private warehouse construction plan
Directly introduce the free programs currently on the market
-
DIY NPM PRIVATE REGIETER
The npm official website provides a solution to build a private warehouse, but this kind of solution is completely built on your own. It is not recommended in terms of time or robustness. Click to view the link
-
Git
Use Git for storage, just specify the URL of the git repository in package.json, but this approach has the following shortcomings
1) Fill in package.json with git warehouses that are not related to this project
2) When the git repository is private, HTTPS or SSH credentials are required, and usually we do not have permissions for each team.
-
Sinopia
The predecessor of verdaccio, but it is currently no longer maintained
-
Cnpmjs.org
cnpm is more troublesome to build, check git
-
Verdaccio is a simple, local private npm repository registry without configuration . You don't need a database to start! Verdaccio provides its own small database, as well as the ability to proxy other registries (such as the npmjs.org website), and can also cache downloaded modules.
This is also the recommended solution for building a private npm warehouse in this article
3. Construction steps (non-docker)
-
A server within the company, the company generally applies for it
-
Nodejs installation
-
npm install -g verdaccio
-
Once installed, you need to execute cli most
$> verdaccio warn --- config file - /home/.config/verdaccio/config.yaml warn --- http address - http://localhost:4873/ - verdaccio/4.5.0
You can set the information of the npm registry
npm set registry http://localhost:4873/
Create a .npmc registration file
//.npmrc registry=http://localhost:4873
Or add a configuration in package.json
{ "publishConfig": { "registry": "http://localhost:4873" } }
4. Build based on docker
-
Pull docker image
docker pull verdaccio/verdaccio
-
Run verdaccio with docker
docker run -it --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio
The last parameter defines which image to use. If you did not pull the image, the above code will pull the latest verdaccio image on dockerhub. It
can be said that it is easy to build with docker, basically just run the command.
5. Upload packages
-
Add account
verdaccio allows anyone to create an account. If no configuration file of verdaccio is configured
config.yaml
, by default any developer who has registered with verdaccio has publish permission// 添加账号 npm adduser --registry your_address
-
Add .npmrc
registry=your_address
-
npm publish
Note that you need to modify the version after each update