首页 > 解决方案 > Run another yarn/npm task within a package.json, without specifying yarn or npm

问题描述

I have a task in my package.json "deploy", which needs to first call "build". I have specified it like this:

"deploy": "yarn run build; ./deploy.sh",

The problem is that this hard codes yarn as the package manager. So if someone doesn't use yarn, it doesn't work. Switching to npm causes a similar issue.

What's a good way to achieve this while remaining agnostic to the choice of npm or yarn?

标签: node.jsnpmyarnpkgpackage.json

解决方案


一种简单的方法是使用npm-run-all包,其文档指出:

纱线兼容性

如果使用 Yarn 调用脚本,npm-run-all 将正确使用 Yarn 执行计划的子脚本。

所以你可以这样做:

"predeploy": "run-s build",
"deploy": "./deploy.sh",

predeploy根据您调用deploy任务的方式,该步骤将使用 npm 或 yarn。

我认为最好让 package.json 中的运行保持包管理器不可知,这样它们就不会绑定到特定的包管理器,但是在项目中,同意使用单个包管理器可能是谨慎的,所以你没有处理冲突的锁文件。


推荐阅读