preface
At work, the company has many internal packages that it does not want to publish to the NPM official repository because it may involve some private code that cannot be exposed. For the front end, you can choose to set up an NPM private warehouse on the Intranet. Currently several mainstream solutions: Verdaccio, Nexus, CNPM. You can choose according to your own needs. This article uses CNPM private server construction.
CNPM private server building process
1. Install Node. The new node version has its own NPM
Official website: nodejs.org/zh-cn/
2. Pull the code and make corresponding changes according to the company’s requirements
git clone https://github.com/cnpm/cnpmjs.org.git
Copy the code
3. Modify the configuration file./config/index.js
// Warehouse site access port
registryPort: 7001.// Page access port
webPort: 7002.// If it can be accessed from the external network, comment it; otherwise, it can only be accessed from the internal network
bindingHost: '127.0.0.1'.// Database configuration
database: {
db: 'cnpmjs'./ / database
username: 'root'.// Database user name
password: ' '.// Database password
dialect: 'mysql'.// Database type 'mysql', 'sqlite', 'postgres', 'mariadb'
host: ' '.// Database service address
port: 3306 / / port
}
// Set key to the user name and password, and value to the mailbox
admins: {
harlie: '[email protected]'.fengmk2: '[email protected]'.admin: '[email protected]'.dead_horse: '[email protected]',},// True indicates that only administrators can publish, false indicates that anyone publishing must carry a private identity
enablePrivate: false.// Private identifier prefix
scopes: [ '@harlie'.'@cnpm'.'@cnpmtest'].// Synchronize the upstream registry address of the module
sourceNpmRegistry: 'https://registry.npm.taobao.org'.// Synchronization mode 'none' does not synchronize, 'all' synchronizes all modules from the source Registry periodically, and 'exist' synchronizes only modules already in the database
syncModel: 'exist'.// Synchronization interval
syncInterval: '10m'.Copy the code
4. Create database (MySQL)
create database cnpmjs; // create database use CNPMJS; source cnpmjs.org/docs/db.sql; // Pull the project docs/db.sqlCopy the code
5. Command start execution (by now the private library has been built, is it not complicated)
node dispatch.js
Copy the code
After successful startup, you can view the website page at http://x.x.x.x:7002.
Private library usage
1. Use the NRM mirror source management tool to add a source
NPM install NRM -g NRM add cnpmorg http://42.192.37.59:7001Copy the code
2. Use internal sources
···NPM·· NPM config set registry http://x.x.x.x:7001 ···CNPM··· CNPM config set registry http://x.x.x.x:7001 ··YARN··· · yarn config set registry http://x.x.x.x:7001Copy the code
3. Release the local project package
- If NPM login is shown in the following figure, the login succeeds
- NPM publish package is published
- npm view @harlie/cnpm-test
You can view the details of the published packages. You can search cnPm-test on the website page to view the corresponding version information.
At the end
The code can be viewed at the Git repository: github.com/HarlieYang/…
Public account: Front-end technology research circle