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

在协作开发中发现的问题,因为使用了不同的包管理器,导致在本地开发环境一切正常的代码推送到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)
  }
}
// package.json 添加命令
{
  "scripts": {
    "preinstall": "node ./preinstall.js"
  }
}

在这里插入图片描述

二、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)
}
// package.json 添加命令
{
  "scripts": {
    "preinstall": "node ./preinstall.js"
  }
}

三、only-allow方案

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

// package.json 添加命令仅此一步
{
  "scripts": {
    "preinstall": "npx only-allow yarn"
  }
}

在这里插入图片描述

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值