Code Monkey home page Code Monkey logo

please-upgrade-node's Introduction

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
// by default, will load ./package.json
require('please-upgrade-node')()

// 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 [email protected]: : 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

API

pleaseUpgradeNode([pkgDir = '.'])

You can pass a pkgDir if your package.json is not at the same level:

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

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

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, please-upgrade-node won't work with Node 0.12:

const pleaseUpgradeNode = require('please-upgrade-node') // โ† Will fail and exit here with Node 0.12,
pleaseUpgradeNode() // Message won't be displayed

To avoid any issues, the recommended syntax is require('please-upgrade-node')().

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

please-upgrade-node's People

Contributors

typicode avatar

Watchers

Suhas Karanth avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.