Giter Club home page Giter Club logo

please-upgrade-node's Introduction

Please upgrade Node npm

πŸ’ show a message to your users to upgrade Node instead of a stacktrace

It's common for new Node users to miss or not understand engines warning when installing a CLI. This package displays a beginner-friendly message if their Node version is below the one expected.

$ node -v
12

$ modern-cli
modern-cli requires at least version 16 of Node, please upgrade

Status

This project is considered feature complete and stable. Only maintenance updates will be published. That said since the goal of the project is to check previous versions of Node, code is unlikely to be modified as it needs to work on very old versions.

Usage

npm install please-upgrade-node

Add please-upgrade-node at the top of your CLI

#!/usr/bin/env node
const pkg = require('./package.json')
require('please-upgrade-node')(pkg) // <- Must run BEFORE requiring any other modules

// ...

Set in your package.json the required Node version

{
  "engines": {
    "node": ">=6"
  }
}

Important: >= is the only operator supported by please-upgrade-node (e.g. >=6, >=6.0, >=6.0.0).

Options

You can set custom exitCode and message function if needed

pleaseUpgradeNode(pkg, {
  exitCode: 0, // Default: 1
  message: function(requiredVersion) {
    return 'Oops this program require Node ' +  requiredVersion
  }
})

Important: to keep message function compatible with older versions of Node, avoid using ES6 features like => or string interpolation.

See also

  • xv - βœ… Simple test runner
  • husky - πŸ• Git hooks made easy

please-upgrade-node's People

Contributors

43081j avatar dependabot[bot] avatar typicode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

please-upgrade-node's Issues

don't pass nested require...

I would think that require('please-upgrade-node')('./your-package-root') would be more appropriate, than a nested require, this way the test can run, then the please-upgrade can simply require(...) on its' own.

If you do this, there's no need to restrict syntax availability for the nested package.

Suggested usage errors out

If I try the try the suggested usage require('please-upgrade-node')(), it fails with the following error:

E:\Projects\repos\cosmiconfig\node_modules\please-upgrade-node\index.js:4
  var requiredVersion = pkg.engines.node.replace(">=", "");
                                   ^

TypeError: Cannot read property 'node' of undefined
    at module.exports (E:\Projects\repos\cosmiconfig\node_modules\please-upgrade-node\index.js:4:36)
    at Object.<anonymous> (E:\Projects\repos\cosmiconfig\index.js:2:31)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (E:\Projects\repos\cosmiconfig\test\caches.test.js:6:19)

I added a couple of console.log statements to check what was going wrong. It turns out, please-upgrade-node loads it's own package.json and errors out because it does not have the engines field.

NVM cus get the wrong Node Verison

i use NVM
nvm use 10.0.0 (my default is 8.0.0οΌ‰
process.verison is return 8.0.0

i can just set deffrent default version in deffrent project
it is too complicated

Feature request: .nvmrc

First of all, great little module. I would very much like to use this.

So, I stopped using the engines field on my projects quite a while ago, in favor of an .nvmrc file with the required Node version in it. A simple nvm install or nvm use picks that up and understands what version is needed, which I find very useful. Since I don't want to maintain the right Node version number in 2 places (.nvmrc and package.json), I stopped mentioning it in package.json.

It seems like your module could benefit from supporting .nvmrc on top of the engines field in package.json. What do you think?

bug: Does not handle `>`

Example scenario:

  • user on Node.js v10.24.1
  • package foo: engines.node=>10

The field can be any valid semver range.

Expected behavior:

error:

foo requires at least version 11.0.0 of Node, please upgrade

Actual behavior

Version test succeeds

Failing test-case: 99b6ebd

Fails for version comparisons <10 versus v10+

This line:

var requiredVersion = pkg.engines.node.replace(">=", "");
var currentVersion = process.version.replace("v", "");
if (currentVersion < requiredVersion) { // oops!
  ...
}

…is an error.

'6' < '10.0.0' === false

You cannot use string comparison here, which sorts lexicographically. You'll need a bit more logic to compare version number strings.

Throw an error instead of using process.exit

I'd like to suggest that instead of calling process.exit(), we throw an Error.

As described in node's documentation for process.exit(), using this method is problematic because it disrupts all asynchronous work in progress. The documentation recommends using an error instead:

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

One concern with making this change would be preserving the functionality of opts.exitCode. I believe we can set process.exitCode right before throwing the error, and it should preserve the intended behavior.

If the maintainers are happy with this idea, I can contribute the change in a PR.

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.