强制删除npm_如何强制使用纱线或NPM

强制删除npm

In this short post, I’m going to show you how to prevent the usage of npm or yarn, depending on your needs. Let’s get started!

在这篇简短的文章中,我将向您展示如何根据需要防止使用npmyarn 。 让我们开始吧!

编辑.npmrc (Edit .npmrc)

You might not have this file in your codebase. If this is the case, create this file in the root folder of your application.

您的代码库中可能没有此文件。 在这种情况下,请在应用程序的根文件夹中创建此文件。

It allows us to specify package manager configurations and it is used by both npm and yarn.

它允许我们指定程序包管理器配置, npmyarn都使用它。

Your .npmrc file should have the engine-strict property marked as true.

您的.npmrc文件应将engine-strict属性标记为true

//.npmrc file

engine-strict = true

This option tells the package manager to use the version of the engines we have specified in the package.json file.

此选项告诉程序包管理器使用我们在package.json文件中指定的引擎版本。

编辑package.json (Edit package.json)

Inside your package.json file you should add the engines section if you don’t currently have it.

如果没有,请在package.json文件中添加engines部分。

//package.json
{ 
  ...
  "engines": {
    "npm": "please-use-yarn",
    "yarn": ">= 1.19.1"
  },
  ...
}

In the above code, the package.json file uses a version of yarn 1.19.1 or greater.But for npm we specify a version that doesn’t exist.

在上面的代码中, package.json文件使用的是yarn 1.19.1或更高版本,但是对于npm我们指定了一个不存在的版本。

This way we make sure that when someone tries to use npm instead of yarn, they will receive an error that outputs ‘please-use-yarn‘.

这样,我们可以确保当有人尝试使用npm而不是yarn ,他们将收到输出“ please-use-yarn ”的错误。

运行npm install (Running npm install)

Once you’ve done the above changes, try to run npm install.

完成上述更改后,尝试运行npm install

You will receive an error that prevents you from using npm.

您将收到一条错误消息,阻止您使用npm

npm ERR! code ENOTSUP
npm ERR! notsup Unsupported engine for my-app@0.1.0: wanted: {"npm":"please-use-yarn","yarn":">= 1.19.1"} (current:
 {"node":"12.16.3","npm":"6.14.4"})
npm ERR! notsup Not compatible with your version of node/npm: my-app@0.1.0
npm ERR! notsup Not compatible with your version of node/npm: my-app@0.1.0
npm ERR! notsup Required: {"npm":"please-use-yarn","yarn":">= 1.19.1"}
npm ERR! notsup Actual:   {"npm":"6.14.4","node":"12.16.3"}

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\YourUser\AppData\Roaming\npm-cache\_logs\2020-05-21T10_21_04_676Z-debug.log

This, of course, can be done the other way around if you want to prevent the usage of yarn.

当然,如果要防止使用yarn ,也可以采用其他方法。

结论 (Conclusion)

It is pretty straightforward and easy to ensure that only one package manager must be used inside your project.This will reduce the chance of errors caused by developers that are using different package managers and it is a good practice to standardize the project’s coding rules and management.

确保在项目内部仅使用一个软件包管理器非常简单明了,这将减少使用不同软件包管理器的开发人员导致的错误机会,并且是标准化项目编码规则和管理的良好实践。

You can reach out and ask me anything on Twitter, Facebook and my website.

您可以在TwitterFacebook和我的网站上向我提问。

翻译自: https://www.freecodecamp.org/news/how-to-force-use-yarn-or-npm/

强制删除npm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值