• 如何统一项目中包管理器的使用?


    在协作开发中发现的问题,因为使用了不同的包管理器,导致在本地开发环境一切正常的代码推送到git后在同事的电脑上构建中却意外的失败了,这是为什么呢?我们在最后查看node_module中的内容时发现,由于使用的不同的包管理器导致安装了有差异的依赖最终导致的这个问题。

    这里我们通过一份预安装脚本来限制使用相同的包管理器,同时使用yarn.lock或package-lock.json来锁定我们的依赖版本。

    以下介绍几种解决方案

    一、UserAgent方案

    1. 通过npm_config_user_agent来获取当前执行的是包管理器的名称和版本
    2. 通过对比名称来限制非允许的包管理器执行安装

    完整代码

    // preinstall.js
    const allowPM = 'yarn'
    const userAgent = process.env.npm_config_user_agent || ''
    if (userAgent !== '') {
      const pmName = userAgent.substring(0, userAgent.indexOf('/'))
      if (pmName !== allowPM) {
        console.warn(
          `\u001b[33m This repository requires using ${allowPM} as the package manager for scripts to work properly.\u001b[39m\n`
        )
        process.exit(1)
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    // package.json 添加命令
    {
      "scripts": {
        "preinstall": "node ./preinstall.js"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    二、ExecPath方案

    1. 通过npm_execpath来获取当前执行的包管理器绝对路径
    2. 通过正则匹配路径中的名称来限制非允许的包管理器执行安装

    完整代码

    const allowPM = 'yarn'
    const execpath = process.env.npm_execpath || ''
    if (!new RegExp(`${allowPM}`).test(execpath)) {
      console.warn(
        `\u001b[33m This repository requires using ${allowPM} as the package manager for scripts to work properly.\u001b[39m\n`
      )
      process.exit(1)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    // package.json 添加命令
    {
      "scripts": {
        "preinstall": "node ./preinstall.js"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    三、only-allow方案

    only-allow为pnpm包管理器组织开源限制方案,only-allow内部使用which-pm-runs来获取当前执行的包管理器后再进行判断拦截,仅需在安装依赖后调整scripts中的内容即可,在vite项目中有使用。

    // package.json 添加命令仅此一步
    {
      "scripts": {
        "preinstall": "npx only-allow yarn"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    但是在 NPM v7 以后的版本中,预安装脚本在安装依赖项后运行,这破坏了预期的行为。
    可以跟踪官方issue的处理进展:https://github.com/npm/cli/issues/2660

  • 相关阅读:
    Linux设备树(Device Tree)何时被解析
    OpenAI 开放 GPT-3 微调功能,让开发者笑开了花
    java基于SpringBoot+Vue的家具销售商城系统 element 前后端分离
    【Docker】之安装 RabbitMQ
    panads操作excel
    微信小程序:页面布局摘要
    【调试笔记-20240602-Linux-在 OpenWRT-23.05 上配置 frps 与 frpc 之间使用 TLS 进行传输】
    华为设备总部与分部配置
    93. 递归实现组合型枚举
    C语言之:数组的定义和初始化必备练习题
  • 原文地址:https://blog.csdn.net/qq_45503196/article/details/125505763