Last active
September 2, 2021 16:41
-
-
Save isaacs/04dc9705e588092809114fe3a45144eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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