简体   繁体   English

NPM 安装添加自定义警告消息

[英]NPM Install add custom warning message

How should I got about adding a warning message when a user tries to install a given version of a library?当用户尝试安装给定版本的库时,我应该如何添加警告消息?

For example, when you install babel-preset-es2015 you get the following warning:例如,当您安装babel-preset-es2015您会收到以下警告:

🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update! 🙌 感谢您使用 Babel:我们现在推荐使用 babel-preset-env:请阅读 babeljs.io/env 更新!

By inspecting their code I saw they add a deprecated entry in their package.json as follows:通过检查他们的代码,我看到他们在 package.json 中添加了一个deprecated条目,如下所示:

"deprecated": "🙌  Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update! ",

I can easily add that, but I'm not actually deprecating anything.我可以很容易地添加它,但我实际上并没有贬低任何东西。 I just want to warn users that they are installing an alpha version and there may be changes in the API.我只是想警告用户他们正在安装一个 alpha 版本,API 可能会有变化。

Question问题

Is there a similar entry to deprecated that can do the job?是否有类似的deprecated条目可以完成这项工作?

There's not necessarily a way you can do that, or not with given fields like "deprecated"不一定有一种方法可以做到这一点,或者不能使用"deprecated"类的给定字段

What you can do, which is a little bit of a workaround, is adding a post-install script , that outputs a string to the console if you mark a version as alpha.您可以做的,这是一种解决方法,是添加一个安装后脚本,如果您将版本标记为 alpha,它将向控制台输出一个字符串。

// package.json
{
  "version": "1.2.3-alpha.2",
  "scripts": {
    "postinstall": "node postinstall.js"
  }
}

// postinstall.js
const package = require('./package.json')

if (package.version.includes('alpha')) {
  console.log('You are using an alpha version. Beware!')
}

Please check out belows:请查看以下内容:

$ npm deprecate <pkg>[@<version>] <message>

Example:示例:

$ npm deprecate some-lib@"< 1.0.0" "🙌 Thanks for using it. we recommend using new version, 1.x.x. Please check out https://example.com"

then,那么,

$ yarn
yarn install v1.16.0
info No lockfile found.
[1/4] 🔍  Resolving packages...
warning some-lib@0.1.5: 🙌 Thanks for using it. we recommend using new version, 1.x.x. Please check out https://example.com
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
✨  Done in 5.25s.
Time: 0h:00m:06s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM