Skip to content

Instantly share code, notes, and snippets.

@rpbeukes
Last active October 1, 2021 08:13
Show Gist options
  • Save rpbeukes/e18b3255f878850511fc32e4eef78adf to your computer and use it in GitHub Desktop.
Save rpbeukes/e18b3255f878850511fc32e4eef78adf to your computer and use it in GitHub Desktop.
Verify node version via npx.
const { exec } = require("child_process");
const {engines} = require('./package');
exec("npx check-node-version --package", (error, stdout) => {
const requiredNodeVersion = engines.node;
if (error) {
console.error(stdout);
exec("node --version", (error, stdout) => {
if (error) {
throw Error(error);
}
const currentNodeVersion = stdout;
const exactRequiredVersion = '10.24.1'
const mess = `
The current node version ${currentNodeVersion} does not satisfy the required version ${requiredNodeVersion}.
Windows instructions to solve the issue...
1. Install nvm:
Download the 'nvm-setup.zip', extract, execute 'nvm-setup.exe' and follow the instructions.
https://github.com/coreybutler/nvm-windows/releases
2. After installation, close your VS Code (or favorite editor) and re-open again to refresh the new system path.
3. Check successful install of nvm:
cmd: nvm version
output: 1.1.7
4. Select or install of nvm:
cmd: nvm use ${exactRequiredVersion}
failed: node v${exactRequiredVersion} (64-bit) is not installed.
success: Now using node v${exactRequiredVersion} (64-bit)
5. If needed, install node v${exactRequiredVersion}
cmd: nvm install ${exactRequiredVersion}
output: Downloading node.js version ${exactRequiredVersion} (64-bit)...
Complete
Creating C:\\Users\\61452\\AppData\\Roaming\\nvm\\temp
Downloading npm version ${exactRequiredVersion}... Complete
Installing npm v${exactRequiredVersion}...
Installation complete. If you want to use this version, type
nvm use ${exactRequiredVersion}
6. List all the node version
cmd: nvm list
output: 14.16.0
14.14.0
* ${exactRequiredVersion} (Currently using 64-bit executable)
---------------------------------------
Mac and Linux can follow the instructions here to get nvm:
https://github.com/nvm-sh/nvm
`;
// auto select correct version
exec("nvm version", (error, stdout) => {
if (error) new Error(mess);
console.info(`nvm version:\n${stdout}`);
exec("nvm list", (error, stdout) => {
if (error) new Error(mess);
console.info(`nvm list:\n${stdout}`);
const nodeVersionsAvailable = stdout.split('\n')
.map(v => v.trim().replace('\n',''));
nodeVersionsAvailable.forEach(v => {
if (v) {
const major = v.substr(0, v.indexOf('.')) ;
if (major.includes('* 10')) {
// already selected, nothing to do here.
return;
}
if (major.includes('10')){
// nvm use the node version
exec(`nvm use ${v}`, async (error, stdout) => {
if (error) new Error(mess);
console.info(`nvm use ${v}:\n${stdout}`);
// wait for the 1 second, otherwise the terminal does not fully register it is using a new node version.
// Detail: if you remove this line, it will switch to new node version but the terminal still executes as if it in the old node version
// ...and of course it will then fail. If you re-run npm run start, all will work.
// This little dirty HACK got me past this hurdle.
await sleep(1000);
return;
});
}
}
});
if (error) new Error(mess);
});
});
});
} else {
console.log(`Node version check, ${requiredNodeVersion}, is satisfied...all is well :)\n`);
}
});
const sleep = ms => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment