Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active September 2, 2021 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/04dc9705e588092809114fe3a45144eb to your computer and use it in GitHub Desktop.
Save isaacs/04dc9705e588092809114fe3a45144eb to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
/*
put this in your package.json
"scripts": {
"preinstall": "node node-version-preinstall.js"
}
*/
const nodeVersion = process.version
const semver = require('semver')
const { engines: { node: nodeRequired, npm: npmRequired }} = require('./package.json')
if (nodeRequired && !semver.satisfies(nodeVersion, nodeRequired)) {
throw new Error(`this package does not work on node version ${nodeVersion}, please update to ${nodeRequired} and try again`)
}
if (process.env.npm_execpath && npmRequired) {
const { execSync } = require('child_process')
const npmVersion = execSync(`${process.execPath} ${process.env.npm_execpath} -v`).toString().trim()
if (!semver.satisfies(npmVersion, npmRequired)) {
throw new Error(`this package does not work on npm version ${npmVersion}, please update to ${npmRequired} and try again`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment