background

I recently came across a question: Some projects do not have the automatic deployment platform similar to Jenkins. Front-end deployment involves the local execution of the NPM run build command to generate a compressed package, and then put it on the server and decompress it to the nginx directory. However, sometimes there is a problem. For example, a member of the project may upgrade a packageA component written by another group, but other members may not upgrade, leading to some problems after the launch. A package is ok, but if several packages are not upgraded, it may cause some unexpected problems.

Train of thought

First of all, on this issue, I think after the following questions

  • package.jsonSeveral versions of the package name in^x.x.xIs not conducive to judgment
  • How do I compare package versions

To solve

Question 1

The solution to the first problem is to directly use X.X.X, a package must be updated only when at least one person knows about it, so the package name can be without any identification

Question 2

On the second question

  • node_modulesThe version in is low, butpackage.jsonIs the latest version, so we can customize a script to determine whether the two versions are consistent
const path = require('path'); const projectRoot = process.cwd(); const projectPack = require(path.resolve(projectRoot, 'package.json')); // Const modules = [' elder-ui ']; const allDependencies = { ... (projectPack.dependencies || {}), ... (projectPack.devDependencies || {}), }; modules.forEach(module => { try { const packVersion = allDependencies[module]; // Some package names have folders such as @vue/cli-service in @vue folder const realPath = packversion.aplit ('/'); const pack = require(path.resolve( ... [projectRoot, 'node_modules', ...realPath, 'package.json'], )); const needVersions = packVersion.match(/\d+/g); const realVersions = pack.version.match(/\d+/g); for (const i in needVersions) { if ( isDef(needVersions[i]) && isDef(realVersions[i]) && parseInt(realVersions[i]) < parseInt(needVersions[i]) ) { exit(module); } } } catch (error) { exit(module); }}); Function exit(moduleName) {throw new Error(' ${moduleName} is too low, please execute NPM I/NPM install '); } function isDef(num) { return num ! == null && num ! == undefined; }Copy the code

Package. json upgrade added the Check script

 "scripts": {
    "check": "node build/checkNpmPackageVersion.js"."dev": "npm run check && vue-cli-service serve"."build": "npm run check && vue-cli-service build"."lint": "vue-cli-service lint"
  },
Copy the code

insufficient

  • If the bag name is not pure numbers, there is a problem with the comparison, because our company does not carry the bag namealpha.betaEtc., so I didn’t consider
  • There are some things to consider in comparison to remote repositories, such as tuningnpm infoThe package name command compares the results

supplement

  • You can use this version if you want to compare it with a remote repositorynpm view axios --jsonThis returns a JSON file

If there is anything that needs to be improved, can we talk more