强制删除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!
在这篇简短的文章中,我将向您展示如何根据需要防止使用npm或yarn 。 让我们开始吧!
编辑.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.
它允许我们指定程序包管理器配置, npm和yarn都使用它。
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.
您可以在Twitter , Facebook和我的网站上向我提问。
翻译自: https://www.freecodecamp.org/news/how-to-force-use-yarn-or-npm/
强制删除npm