Why build?

NPM – we all know NodeJS package management tool, used for Node plug-in management including installation, uninstall, manage dependencies, etc.

Based on the NPM command line, we can quickly install the code modules that depend on the project, and even release some plug-ins written by ourselves. So that our project development efficiency has been greatly improved.

Modules published to NPM are open source, so we can only share open source modules that everyone can use.

Today, however, as businesses become more complex and projects iterate faster, it becomes necessary to share common business code across projects. Of course we can’t open source the business code of the company. At this time, it is necessary to build a private NPM repository similar to the NPMjs.org platform, which is used for the storage and quick installation of common business modules, such as business components, in enterprises.

Generally speaking, we want to build an internal NPM warehouse, and use the command line tool of NPM to reuse business code modules or business components quickly while managing packages by ourselves.

How to set up?

Mainstream private NPM warehouse construction schemes in the industry are as follows:

Pay to buy using Git + SSH this way directly to the GitHub project address using Sinopia using cnpmjs.org this article will focus on the fourth option.

Local test environment:

Window10 node V8.10.0 NPM v5.6.0 mysql V8.0

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Install or download cnpmjs.org

Install through NPM, install with version number, if there is no version number currently installed 2.19.4. The beta database has 16 tables, compared to 14 for 2.x.

NPM install -g [email protected] Download cnpmjs.org from github

Github.com/cnpm/cnpmjs…

2. Modify the configuration

Open the configuration file: cnpmjs.org/config/inde…

Part of the configuration instructions as follows, from www.colabug.com/2731929.htm…

/* * webPort: 7002, //' '/** * database config // database: {db:'cnpmjs'// Database name username:'root'// database access password:'123456'// The SQL dialect of the database // - currently supported:'mysql'.'sqlite'.'postgres'.'mariadb'
    dialect: 'mysql'// select * from mysql; // select * from mysql; Default: 127.0.0.1 host:'127.0.0.1', // Database access IP, usually 127.0.0.1 // custom port; Default: 3306 port: 3306, // Database access port, usually 3306 // module file storage. By default, published private modules and cache public modules are stored in the local file system. The path is ~/.cnpmjs.org/nfs. Or you can choose three-way storage, such as seven cows, etc., where the plug-in is configured; Also supports interface development extension storage; nfs: require('fs-cnpm')({
    dir: path.join(dataDir, 'nfs'}), // registry url name // registry url name = r.cnpmjs.org' 'Admins: {// name: email //fengmk2:'[email protected]',
    admin: '[email protected]',
    //dead_horse: '[email protected]'}, /* * registry mode config Specifies whether to enable private mode. The default value isfalse; // In private mode, only administrators can publish modules, and other accounts have synchronization permission. // In non-private mode, registered users can publish modulesenablePrivate: false, // registry // This is mandatory if published in non-private mode. Non-admin published module names must start with the scopes field, Module named sample "@ CNPM/packagename" / / know more about NPM - scope, please refer to https://docs.npmjs.com/misc/scope scopes: ['@cnpm'.'@cnpmtest'.'@cnpm-test'], // Private modules will not be whitelisted for scopes, whitelisted for various old modules that are not published by scope, and privatePackages maintained in array form: [], /** * sync configs synchronizes source repository Settings */ // NPM official Registry address. Modules are not synchronized directly from this address, but module information is sometimes retrieved from this address. Do not change the official registry unless necessary:'https://registry.npmjs.com',
officialNpmReplicate: 'https://replicate.npmjs.com'// Synchronize the upstream registry address of the modulesourceNpmRegistry: 'https://registry.npm.taobao.org', // Whether the upstream registry is CNPM, defaulttrueTo use the NPM official address as the upstream of synchronization, set it tofalse
sourceNpmRegistryIsCNpm: true// If the module does not exist at the time of installation, whether to synchronize to the source Registrytrue
syncByInstall: true// exist: synchronizes only the modules already in the database. // all: synchronizes all the modules in the source Registry syncModel:'exist', / /'none'.'all'.'exist'// Synchronization interval (default: 10 minutes) syncInterval:'10m'// Whether to synchronize devDependencies in the module, defaultfalse
syncDevDependencies: false, / / user account system access, can expand access to company's account system / / see the https://github.com/cnpm/cnpmjs.org/wiki/Use-Your-Own-User-Authorization userService: null,enableAbbreviatedMetadata: true.Copy the code

The most important configuration is database configuration. Generally, the database can be started properly if it is correctly configured.

3. Import the database

Enter the database (how to enter baidu);

Create a database;

create database cnpmjs;
Copy the code

Switch to the CNPM database

use cnpmjs;
Copy the code

Import the CNPM database configuration file

The file is located under the CPM installation directory docs/db.sql

source docs/db.sql;
Copy the code

View the imported table

I imported 16 tables in this version

show tables;
Copy the code
  1. Since cnpmjs.org itself does not support the Window environment, starting at Window requires some modifications

You can directly

node dispatch.js
Copy the code

You can also modify package.json

"dev": "DEBUG=cnpm* node dispatch.js" 
Copy the code

Instead of

"dev": "set DEBUG=cnpm* node dispatch.js" 
Copy the code

After startup access IP :7002 the following page is displayed

  1. Install CNPM using CNPM
npm i -g cnpm
Copy the code

Point CNPM Registry to our private NPM service IP

cnpm config set registry ip:7001
Copy the code

CNPM is then used in the same way as NPM, logging in, publishing packages, etc

Problem summary: In the process of connecting to the database, the connection fails all the time. After consulting the data, it is found that the problem is the mysql version. If you use mysql of a higher version, the authentication failure will occur.

UPDATE mysql.user SET password=PASSWORD('newpassword') WHERE User='root';
Copy the code