Benefits of setting up private NPM

NPM stands for Node Package Manager. The benefits of setting up a private NPM include:

  1. Improve code reuse and increase team sedimentation
  2. Stripping project dependence, engineering more lightweight
  3. Reference full update, support version degradation
  4. Establish module documentation to reduce the difficulty of getting started
  5. Complete code quality control, no need to repeat the test
  6. There is a growing trend for build tools that optimize the release process, reduce manual work, and increase team productivity
  7. Enhance team code communication
  8. Internal confidentiality mechanism

Work to be done

  1. Set up a private NPM environment
  2. Explore NPM using workflow
  3. NPM interconnects with OA and performs permission control
  4. NPM upload specification developed
  5. Existing components uploaded to NPM for modification
  6. Use Git to maintain the source code base
  7. Git interconnects with NPM publish to automatically test, build, publish, and roll back

Private NPM build based on CNPM

Why CNPM?

  1. Taobao product has passed the test of large flow
  2. Most companies in the country deploy private NPM options
  3. Complete interface, reduce secondary development difficulty
  4. Friendly deployment, support MSYQL, convenient data migration
  5. More complete Markdown support, documentation friendly
  6. Perfect client support, rich workflow options
  7. There really aren’t many other solutions, official tools need to use coachDB, Sinopia is too mini, doesn’t have database support, and doesn’t support Markdown well

The deployment process

// TODO // TODO:

Have a problem

  1. The company prefix needs to be defined and the project is more intrusive. The project using the private NPM must use CNPM installation or specify Registry installation. As a result, the package of the public module will also request to the private NPM, and the private NPM will request from the Taobao source, which may cause a certain delay
  2. You need to specify the administrator user in the configuration. Only the administrator user can publish projects. You need to explore whether oa authentication can be accessed
  3. The default interface is ugly, we can optimize it and make our own markup

Private NPM uses workflow

Several ways to use private NPM

NPM has a configuration item called Registry, similar to the concept of software sources in Linux. This configuration specifies which address the NPM should use as the remote repository for downloading and publishing the code.

So, to use the private NPM, we will change the regispop configuration. NPM gives us two ways to do this (there are more than one, I use the following two most often) :

  1. Modify the NPM global configuration as follows:

  2. npm config set registry npm.corp.chinahr.com

The advantage of this is that the address can be modified once and used permanently without needing to be set again. The disadvantage is that the address can not be accessed anytime and anywhere, because it is the Intranet address of the company, which is more intrusive for developers.

  1. The –registry parameter is followed each time the NPM command is executed.

  2. npm install vue –registry npm.corp.chinahr.com

The advantage of doing this is that it is flexible and can optionally specify the Registry address, and it takes effect this time. The disadvantage is that you need to manually enter the Registry address each time.

So neither of these are perfect solutions, and CNPM also offers two other solutions:

  1. Install the CNPM client (also an NPM program), which contains all NPM commands, set the registry of the client to the address of the private NPM, and use CNPM instead.
  2. Set up a new alias command that sets registry for private NPM as the default and uses this alias later instead of the NPM verbose setting method.

All four methods can be used using private NPM. The disadvantages of each method are:

  1. If there is no internal network environment, NPM cannot be used and needs to be switched back.
  2. Cumbersome to use;
  3. [Fixed] A new command is not compatible with existing scaffolding and automation tools.
  4. Same as 3, and not Windows friendly.

So there is still no perfect solution, and the most recommended solution is 3

In subsequent documentation, the CNPM command represents proper use of private NPM

Download package

NPM install is no different from NPM install, except that the package name is preceded by the company prefix @chr/. This is because CNPM is positioned as a full synchronization tool for NPM images. This is to prevent conflicts with the package in nPMjs.org. There is a package called Webpack in the private NPM that you need to download like this:

  1. cnpm install @chr/webpack

Release the package

The release package needs to be authorized in the configuration file in advance. After our OA access, the permission can be configured so that everyone can release the package. Steps to publish the package:

  1. CNPM init creates package.json in this module and makes the files in this folder managed by NPM. Note that the project name should be followed by @chr/
  2. Modify the project’s README file to give a brief description of the project
  3. If the project is to be released for the first time, the version number is not needed. If the project is to be updated, change the version number first. Otherwise, the project cannot be released
  4. CNPM login Logs in to NPM on the cli, which will be associated with OA in the later stage. In the current state, if no user is automatically registered, it is recommended to use an OA account for future interconnection. 5. CNPM publish package

Problems to be solved

  1. NPM automatically installs the latest version of package, and if a package version update notification is not done properly, existing code may be affected as a result of interface upgrades

Publish the package specification

The biggest significance of building private NPM is to reuse our core code and improve the development efficiency. Therefore, there should be a strict specification for the uploaded code, which can ensure the code quality, reduce the communication cost and prevent confusion in collaboration.

This specification needs to be discussed together. Here is an example

README.md

Documentation is very important for modular projects. Through documentation, callers can see the basic functions of the project at a glance, and quickly understand what problems the project solves, how to invoke, and what problems exist, without having to communicate with the project maintainer. NPM has a default package from the document management scheme, that is, through the readme.md project document within the project should contain at least the following aspects:

  1. What problem was solved
  2. Which components to include
  3. How to call
  4. What are the configuration items
  5. What are the problems
  6. Update plan: long term/rapid iteration

example

Sample programs for the various methods in the project should be included

test

As a basic module, unit testing is indispensable, and the basic module is also very suitable for unit testing

You can explore the testing area together

review

Updates and releases of important modules should be reviewed by the majority of the team

npm script

Update notification