npm - 防止 Yarn 安装在项目中运行(即强制 NPM 安装)

标签 npm npm-install yarnpkg

我们最近从 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/

相关文章:

javascript - 如何检查本地 JavaScript 包是否是 Semver?

javascript - 如何发布和使用 npm 包

node.js - ./src/styles.scss 中的错误 .. 模块构建失败 : Error: Cannot find module 'node-sass'

angular - yarn 错误未知工作区 - 为什么 `yarn workspace` 会因 `nohoist` ed 包而失败?

ruby-on-rails - 未检测到 Rails 预编译 Assets Yarn 可执行文件

npm - 使用npm或yarn显示包的当前版本的命令?

npm - 为什么我不能通过 child_process.exec 调用运行 npm install? npm exit 243 没有输出

javascript - 有没有办法将类似功能的 Node 模块组合起来?

ubuntu - Vagrant - npm install 不起作用

javascript - 使用 npm 安装本地模块?