Be friendly, be cool 😎 show a message to your users to upgrade Node instead of a stacktrace
JavaScript
Switch branches/tags
Nothing to show
Latest commit 933f7a6 Jul 10, 2017 @typicode 1.0.1
Permalink
Failed to load latest commit information.
.gitignore first commit Jul 8, 2017
.npmignore first commit Jul 8, 2017
.travis.yml first commit Jul 8, 2017
LICENSE first commit Jul 8, 2017
README.md Update README.md Jul 8, 2017
index.js update message Jul 10, 2017
package-lock.json 1.0.1 Jul 10, 2017
package.json 1.0.1 Jul 10, 2017
test.js first commit Jul 8, 2017

README.md

Please upgrade Node Build Status npm

Be friendly, be cool 😎 show a message to your users to upgrade Node instead of a stacktrace

It's common for new Node users to miss the npm engines warning when installing a CLI. This package displays a beginner-friendly message if their Node version is below the one expected and it's just a few lines of JS code.

Example with hotel CLI:

$ node -v
0.12
$ hotel
hotel requires at least version 4 of Node, please upgrade

Usage

Install:

npm install please-upgrade-node

yarn add please-upgrade-node

Require please-upgrade-node before anything else:

// bin.js
require('please-upgrade-node')(
  require('./package.json')
)

// The rest of your code...

In your package.json, define the required Node version:

{ 
  "name": "super-cli",
  "bin": "./bin.js",
  "engines": { "node": ">=6" }
}

Please note that >= is the only operator supported by please-upgrade-node (e.g. >=4, >=4.0, >=4.0.0). Now if your users install your super-cli project with an older Node version, they'll see:

$ npm install -g super-cli
# [...]
npm WARN engine super-cli@1.0.0: : wanted: {"node":">=6"} (current: {"node":"4.8.3","npm":"2.15.11"})
# [...]
$ super-cli
super-cli requires at least version 6 of Node, please upgrade

Caveat

Make sure when requiring please-upgrade-node to not use syntax that is only supported in recent versions of Node.

For example, if you use const instead of var and don't transpile it, please-upgrade-node won't work with Node 0.12:

const pkg = require('./package.json') // ← Will fail and exit here with Node 0.12,
                                      // because const isn't supported.
require('please-upgrade-node')(pkg)   // No upgrade message will be displayed :(

See also

  • pkg-ok - πŸ‘Œ Prevents publishing a module with bad paths
  • husky - 🐢 Git hooks made easy

Thanks to zeit/serve for inspiring the error message.

License

MIT - Typicode 🌡 - Patreon