强制 yarn install 而不是 npm install 用于 Node 模块? [英] Force yarn install instead of npm install for Node module?

查看:161
本文介绍了强制 yarn install 而不是 npm install 用于 Node 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强制使用 yarn install 而不是 npm install.我想在 npm install 中引发错误.我应该在 package.json 中做什么?

I want to force using yarn install instead of npm install. I want to raise error in npm install. What should I do in package.json?

推荐答案

更新: 亚历山大的回答是更好的解决方案,并使用我在此处描述的相同技术.我将机智地为后人留下我的答案.我的回答的原意是表明您可以执行一个应该适用于所有平台的小型节点脚本.

UPDATE: Alexander's answer is the better solution and uses the same technique I describe here. I am leaving my answer in tact for posterity. The original point of my answer was to show that you can execute a small node script which should work on all platforms.

在你的预安装脚本中,你可以运行一个迷你节点脚本,它应该适用于所有平台,而像 pgrep(和其他常见的 *nix 命令和操作符)这样的东西在 Windows 之前不会在 Windows 上运行10 已被广泛采用.

In your preinstall script you can run a mini node script which should work on all platforms, whereas things like pgrep (and other common *nix commands and operators) won't work on Windows until Windows 10 has received widespread adoption.

我在 Node v4.7.0 (npm v2.15.11) 和 Node v7.2.1 (npm v3.10.10) 上测试了以下脚本.我认为它适用于介于两者之间的所有内容.它通过检查当前正在运行的进程上的环境变量来工作 - npm_execpath 是当前运行的npm"脚本的路径.对于yarn,它应该指向/path/to/yarn/on/your/machine/yarn.js.

I tested the below script on Node v4.7.0 (npm v2.15.11) and Node v7.2.1 (npm v3.10.10). I assume it works on everything in between. It works by checking the environment variables on the currently running process - the npm_execpath is the path to the currently running "npm" script. In the case of yarn, it should point to /path/to/yarn/on/your/machine/yarn.js.

"scripts": {
    "preinstall": "node -e \"if(process.env.npm_execpath.indexOf('yarn') === -1) throw new Error('You must use Yarn to install, not NPM')\""
}

您可以在此处阅读有关 npm 脚本的更多信息:https://docs.npmjs.com/misc/scripts

You can read more about npm scripts here: https://docs.npmjs.com/misc/scripts

至于 npm_execpath 环境变量,虽然没有记录,但我怀疑它是否会改变.它已经出现在 npm 的多个主要版本中,但它并没有真正通过 有一个更好的名称" 测试.

As far as the npm_execpath environment variable, while not documented I doubt that it will ever change. It's been around for multiple major releases of npm and it doesn't really pass the "there's a better name for this" test.

这篇关于强制 yarn install 而不是 npm install 用于 Node 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆