1. Introduction

Reading this article, you will learn:

1. NPM script hook: preinstall; 2. The NPX command; 3. Only - allow library; 4. One line of code 'preinstall':' NPX only-allow NPM 'implementation constraint use NPM package manager;Copy the code

2. The scene

Dependencies need to be installed during front-end project development. Dependencies can be installed in various ways, such as NPM, YARN, and PNPM. When the team have many people in the coordinated development, be sure to is to use the same package manager, is common practice in the md document together, but there is always someone don’t look at the document, or a lapse USES another package manager, for example in the project using the NPM, while a person USES the yarn installed dependence, and upload the code, This makes it possible for the project to be buggy due to the package version, and today we will use a single line of code to enforce constraints to solve this problem.

3. Environment preparation

Node v14.16.0 2. NPM v6.14.11 3. Yarn v1.22.11 4Copy the code

4. Code demonstration

4.1 newlimit-packagefolder

mkdir limit-package && cd limit-package
Copy the code

4.2 Initializing the Project

// -y omits its own step to set the project name NPM init -yCopy the code

4.3 Adding script Scripts

"Scripts ": {"test": "echo "Error: no test specified\" && exit 1", "preinstall":" NPX only-allow NPM "// added},Copy the code

New line “preinstall”: “NPX only-allow NPM”, meaning that only NPM manager is allowed.

4.4 Verification Results

Check whether the dependency can be successfully installed using the Yarn manager. Run yarn add chalk -d in the root directory (Chalk is an NPM package with rich printing contents). At this time, the terminal displays an error message:

Use “NPM install” for installation in this project error message is displayed, and the node_modules folder and yarn.lock file are not displayed in the project. The yarn package manager is successfully restricted.

Yarn has limited usage, so we will try NPM to install the dependency. If the dependency can be successfully installed, the desired function has been achieved.

NPM install chalk -d:

As can be seen from the above two images, the package Chalk has been successfully installed, indicating that the NPM package manager can be used normally.

Principle 5.

“Preinstall “:” NPX only-allow NPM”

First, preinstall is an official hook provided by NPM. When NPM install or YARN Add is executed, the script corresponding to Preinstall is triggered.

NPX only allow NPM

Since version 5.2, NPM has added the NPX command, which is mainly used to call the module installed by the project and execute the binary files in the module. For details, you can see Ruan Yifeng’s article. Simply speaking, it is to execute the code of the package only-allow. Here is the Github address of the only-allow package.

6. Summary

In this article, we learned that by starting with a real scenario where a team needs to standardize a unified package manager, one line of code “preinstall”: “NPX only-allow NPM” can implement the requirements. We also learned about the use of the NPX command, learned about the use of the NPM preinstall hook, and learned about the use of the only-allow package. In fact, the only-allow package is also used in the source code, as shown in the following figure: