Repository: yargs/yargs-parser
Pull request #278 (2020-06-04 16:06:14)

build!: drops Node 6. begin following Node.js LTS schedule
This skips the EOL Node 8, and moves us to Node 10 as the supported Node.js version for yargs-parser.

_Note: we should complete the TypeScript conversion before we release this major version of the library._

TODO:

- [x] I'd like to add a snippet of code that throws if you're on an older Node.js version.

Refs #272

QmarkC: > TODO:
> 
> * [ ]  I'd like to add a snippet of code that throws if you're on an older Node.js version.

Run time or build time e.g. in pretest?
Basic check such as parsing [`process.version`](https://nodejs.org/docs/latest-v10.x/api/process.html#process_process_version) or add a dep like [`check-node-version`](https://www.npmjs.com/package/check-node-version)?


bcoe: @QmarkC I think it's worth adding something along the lines of:

```
const minNodeVersion = 10;
if (process && process.version) {
  const major = Number(process.version.match(/v([^.]+)/)[1]);
  if (major < minNodeVersion) {
    throw Error(`yargs parser supports a minimum Node.js version of ${minNodeVersion}`)
  }
}
```

Patch:
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 8241646..0213d80 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -9,7 +9,7 @@ jobs:
     runs-on: ubuntu-latest
     strategy:
       matrix:
-        node: [8, 10, 12, 13]
+        node: [10, 12, 14]
     steps:
       - uses: actions/checkout@v1
       - uses: actions/setup-node@v1
diff --git a/README.md b/README.md
index bae61c2..eff195c 100644
--- a/README.md
+++ b/README.md
@@ -439,6 +439,12 @@ node example.js --unknown-option --known-option 2 --string-option --unknown-opti
 { _: ['--unknown-option'], knownOption: 2, stringOption: '--unknown-option2' }
 ```
 
+## Supported Node.js Versions
+
+Libraries in this ecosystem make a best effort to track
+[Node.js' release schedule](https://nodejs.org/en/about/releases/). Here's [a
+post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
+
 ## Special Thanks
 
 The yargs project evolves from optimist and minimist. It owes its
diff --git a/index.js b/index.js
index c14c1fc..24962d6 100644
--- a/index.js
+++ b/index.js
@@ -4,6 +4,17 @@ const path = require('path')
 const tokenizeArgString = require('./lib/tokenize-arg-string')
 const util = require('util')
 
+// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our
+// version support policy. The YARGS_MIN_NODE_VERSION is used for testing only.
+const minNodeVersion = (process && process.env && process.env.YARGS_MIN_NODE_VERSION)
+  ? Number(process.env.YARGS_MIN_NODE_VERSION) : 10
+if (process && process.version) {
+  const major = Number(process.version.match(/v([^.]+)/)[1])
+  if (major < minNodeVersion) {
+    throw Error(`yargs parser supports a minimum Node.js version of ${minNodeVersion}. Read our version support policy: https://github.com/yargs/yargs-parser#supported-nodejs-versions`)
+  }
+}
+
 function parse (args, opts) {
   opts = Object.assign(Object.create(null), opts)
   // allow a string argument to be passed in rather
@@ -626,8 +637,8 @@ function parse (args, opts) {
   }
 
   function applyEnvVars (argv, configOnly) {
+    if (!process) return
     if (typeof envPrefix === 'undefined') return
-
     const prefix = typeof envPrefix === 'string' ? envPrefix : ''
     Object.keys(process.env).forEach(function (envVar) {
       if (prefix === '' || envVar.lastIndexOf(prefix, 0) === 0) {
diff --git a/package.json b/package.json
index 7bf3236..1acaabc 100644
--- a/package.json
+++ b/package.json
@@ -41,6 +41,6 @@
     "index.js"
   ],
   "engines": {
-    "node": ">=6"
+    "node": ">=10"
   }
 }


Test patch:
diff --git a/test/yargs-parser.js b/test/yargs-parser.js
index f5cbc18..e017a08 100644
--- a/test/yargs-parser.js
+++ b/test/yargs-parser.js
@@ -3823,4 +3823,13 @@ describe('yargs-parser', function () {
       }
     })
   })
+
+  it('throws error for unsupported Node.js versions', () => {
+    process.env.YARGS_MIN_NODE_VERSION = '55'
+    delete require.cache[require.resolve('../')]
+    expect(() => {
+      require('../')
+    }).to.throw(/yargs parser supports a minimum Node.js version of 55/)
+    delete process.env.YARGS_MIN_NODE_VERSION
+  })
 })