Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

šŸ™Œ Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!

By inspecting their code I saw they add a deprecated entry in their package.json as follows:

"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.

Question

Is there a similar entry to deprecated that can do the job?

like image 439
Alan Souza Avatar asked Oct 25 '17 21:10

Alan Souza


People also ask

Why npm install deprecated warnings?

The npm WARN deprecated message means that the package installed on your project is not recommended for use. This may be due to issues found with the package or it's no longer maintained. You may not even see these packages in your package.


2 Answers

There's not necessarily a way you can do that, or not with given fields like "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.

// 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!')
}
like image 91
pixeldesu Avatar answered Sep 19 '22 10:09

pixeldesu


Please check out belows:

  • https://docs.npmjs.com/cli/deprecate
  • https://docs.npmjs.com/deprecating-and-undeprecating-packages-or-package-versions
$ 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 [email protected]: šŸ™Œ 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
like image 45
aluc Avatar answered Sep 18 '22 10:09

aluc