All Projects â†’ transitive-bullshit â†’ node-compat-require

transitive-bullshit / node-compat-require

Licence: other
Easily allow your Node program to run in a target node version range to maximize compatibility.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to node-compat-require

shoulders
💛 Quickly view a list of your dependencies' open issues.
Stars: ✭ 62 (+181.82%)
Mutual labels:  npx
oconfigure
configuration script for BSD.lv projects
Stars: ✭ 36 (+63.64%)
Mutual labels:  compatibility
nativescript-appversion
đŸ”ĸ NativeScript plugin to retrieve your app's package ID and current version
Stars: ✭ 47 (+113.64%)
Mutual labels:  version
license-compatibility
Šī¸ Check compatibility between different SPDX licenses
Stars: ✭ 31 (+40.91%)
Mutual labels:  compatibility
coverage-node
A simple CLI to run Node.js and report code coverage.
Stars: ✭ 39 (+77.27%)
Mutual labels:  npx
version-compare
↔ī¸ Rust library to easily compare version strings. Mirror from https://gitlab.com/timvisee/version-compare
Stars: ✭ 32 (+45.45%)
Mutual labels:  version
emulatetab
A jQuery plugin to emulate tabbing between elements on a page.
Stars: ✭ 15 (-31.82%)
Mutual labels:  compatibility
repology-rules
Package normalization ruleset for Repology
Stars: ✭ 67 (+204.55%)
Mutual labels:  version
bump
a tiny tool to bump nimble versions đŸģ
Stars: ✭ 23 (+4.55%)
Mutual labels:  version
app-version-laravel
Laravel application versioning
Stars: ✭ 24 (+9.09%)
Mutual labels:  version
next-ver
Tells you the next semantic version for your local package
Stars: ✭ 27 (+22.73%)
Mutual labels:  version
parver
Parse and manipulate version numbers.
Stars: ✭ 41 (+86.36%)
Mutual labels:  version
pg global temp tables
Oracle-style global temporary tables for PostgreSQL
Stars: ✭ 16 (-27.27%)
Mutual labels:  compatibility
compat-db
A browser API compatibility database
Stars: ✭ 61 (+177.27%)
Mutual labels:  compatibility
bump
A generic version tracking and update tool
Stars: ✭ 33 (+50%)
Mutual labels:  version
PHPUnit-Polyfills
Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests
Stars: ✭ 147 (+568.18%)
Mutual labels:  compatibility
pie-my-vulns
Visualize your project security vulnerabilities as a pie chart in the terminal
Stars: ✭ 23 (+4.55%)
Mutual labels:  npx
webapp-wordlists
This repository contains wordlists for each versions of common web applications and content management systems (CMS). Each version contains a wordlist of all the files directories for this version.
Stars: ✭ 306 (+1290.91%)
Mutual labels:  version
exec
🐚 semantic-release plugin to execute custom shell commands
Stars: ✭ 94 (+327.27%)
Mutual labels:  version
zerover
0ī¸âƒŖ Minimalist versioning scheme for devs who can't be bothered.
Stars: ✭ 141 (+540.91%)
Mutual labels:  version

node-compat-require

Easily allow your Node program to run in a target node version range to maximize compatibility.

NPM Build Status JavaScript Style Guide

  • If unsupported node version, installs the right version with npx and continues.
  • Targeted at CLIs that want to maximize compatibility without sacrificing JS features.
  • Use async / await in older versions of node.
  • Optionally pin your node program to a specific version of node for extreme reproducibility.

Demo

Why

A lot of Node.js CLI programs need to support older versions of Node, and in order to do so, they either:

  • Resort to using deprecated ES5 syntax and carefully make sure all dependencies follow suit.
  • Require the CLI to be run via Docker which is clumsy to execute (eg. no npm install -g).
  • Rely on a complicated transpilation step in order to achieve backwards compatibility.

While transpilation is great for larger projects, it's a bit of a headache, when all you really want to do is ensure your program works for end users.

node-compat-require is the simplest way of ensuring a compatible node version without sacrificing the latest & greatest node features.

Install

This module requires node >= 4.

npm install --save node-compat-require

Usage

const compatRequire = require('node-compat-require')

compatRequire('.', { node: '>= 8' })

In this example, './index.js' would be required only once the Node process is >= 8.

See the example folder for a complete example of a node program which can be run with node >= 4 but will enforce node >= 8 at runtime in order to support newer JS features like async / await and object destructuring.

API

compatRequire(path, opts)

If the current node process satisfies the given requirements, returns require(path).

If the current node process does not satisfy the requirements, installs the correct version of node, re-invokes the current node program as a subprocess, and exits once the child process exits.

path

Type: String Required

Path of file to require if node process satisfies constraints. This may be a relative file just like a normal node require statement.

opts

Type: Object Required

opts.node

Type: String Required

Required semver range for the node process.version.

Examples:

compat('.', { node: '>= 8' })
compat('./bin', { node: '^6' })
compat('./lib/cmd', { node: '9' })
compat('./example/cli', { node: '7.10.0' })
compat('.', { node: '4 || >=9 || 6.0.0 - 7.0.0' })
opts.quiet

Type: Boolean Default: false

Use this to optionally silence the npx output.

How it works

You require node-compat-require and pass a desired node semver range (like '>= 8' or '^6.0.0').

If the current process's node version satisfies that range, then node-compat-require requires the target module and returns.

If the current process does not satisfy that range, then npx is used to temporarily install the appropriate matching version of node from npm and re-run the current process as a subprocess using the temporary node executable. In this case, all commandline flags, environment variables, and stdio will be inherited from the current process. The child process will again run into node-compat-require, only this time it will require your target module normally because the version check is satisfied. Once the child process terminates, either due to successful completion or an error, node-compat-require will exit the parent process with the same exit code.

Note: it is recommended but not required for you to invoke node-compat-require at the very beginning of your program. Note: node-compat-require needs an active internet connection for npx to install the correct version of node.

Related

  • npx - Used under the hood to execute specific versions of node from npm.
  • node - NPM package bundling different versions of node for different platforms.
  • node language features - Breakdown of supported features across different versions of node.

License

MIT Š Travis Fischer

Support my OSS work by following me on twitter twitter

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].