我们最近从 Yarn 切换回使用 NPM,但旧习惯很难改掉,我担心一些开发人员会意外使用 yarn install
。
如何防止yarn install
从在项目中运行?或者,更好的是,显示使用提醒 npm install
?
我在想yarn install
可以用 preinstall
拦截脚本,但我不确定在 preinstall
中寻找什么脚本。
最佳答案
我想到了另一种选择,利用 Yarn 会根据 package.json
中的 $.engines.yarn
检查其版本。如果您按如下方式设置:
{
...
"engines": {
"yarn": "use npm!"
}
}
Yarn 将会退出,尽管会出现一个有点神秘的错误消息:
yarn install v{foo}
info No lockfile found.
[1/5] 🔍 Validating package.json...
error {bar}@{baz}: The engine "yarn" is incompatible with this module. Expected version "use npm!". Got "{foo}"
error Found incompatible module
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
关于npm - 防止 Yarn 安装在项目中运行(即强制 NPM 安装),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57328593/