You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have created this script and it is for user convince to go to check script
Automatic Node.js Version Checker Script
This JavaScript script automates the process of checking and managing Node.js versions based on specific conditions:
Check Current Node.js Version:
The script begins by checking the currently installed Node.js version on your system.
Comparison with Required Version from .nvmrc File:
If the currently installed Node.js version matches the required version, the script logs a success message and terminates.
Using Node Version from .nvmrc File:
If the installed Node.js version doesn't match the required version, the script looks for a Node.js version specified in a .nvmrc file in your project directory.
If an .nvmrc file is found and contains a valid Node.js version, the script switches to and uses that version.
Installing Missing Node.js Version:
If there's no .nvmrc file or the specified version is not installed, the script prompts the user with a confirmation message.
If the user chooses to install the required version, the script proceeds to install it using a nvm install versioncommand
After installation, the script switche and uses the newly installed version.
Terminating the Script:
If the user chooses not to install the required version, the script gracefully terminates.
This script provides an automated and user-friendly way to ensure that your Node.js environment is set to the required version for your project.
I have tested this script in node v4.9.11 to latest,
It works fine and if not working in any specific version then let me know I will update the script
Prerequisites as follow
Must have nvm installed on your machine
Current in use version of node need to be grater then v4.9.11
Scenario when this would be used?
Can be use before starting your local environment
Can be use to switch version automatically
Supporting information
const childProcess = require("child_process");
const fs = require("fs");
const readline = require("readline");
const execSync = childProcess.execSync;
// Read .nvmrc file
const nvmrcPath = ".nvmrc"; // Replace with your .nvmrc file path
const main = () => {
try {
// Read the content of the .nvmrc file
const nvmrcVersion = fs.readFileSync(nvmrcPath, "utf-8").trim();
// Get the current version of Node.js installed on the system
const currentVersion = execSync("nvm current", { encoding: "utf-8" }).trim();
if (currentVersion !== nvmrcVersion) {
console.log("Current Node.js version:", currentVersion);
console.log("Trying to use .nvmrc file version:", nvmrcVersion);
try {
nvmUseVersion(nvmrcVersion);
} catch (err) {
console.error("Error using Node.js version from .nvmrc:", err);
console.log(`Please manually run 'nvm use ${nvmrcVersion}' to switch to the desired version.`);
}
} else {
console.log("Node.js versions match:", currentVersion);
}
} catch (err) {
console.error("Error reading .nvmrc file:", err);
}
};
const requestToInstallNewVersion = (nvmrcVersion) => {
// Prompt the user for installation confirmation
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Do you want to install this version? (y/n): ", (answer) => {
rl.close();
if (answer.toLowerCase() === "y") {
try {
// Install the desired version using `nvm install`
const installOutput = execSync(`nvm install ${nvmrcVersion}`, { encoding: "utf-8" }).trim();
console.log(installOutput);
nvmUseVersion(nvmrcVersion);
} catch (err) {
console.error("Error installing Node.js:", err);
}
}
});
};
const checkVersionAfterUpdate = (nvmrcVersion) => {
const updatedVersion = execSync("nvm current", { encoding: "utf-8" }).trim();
if (updatedVersion !== nvmrcVersion) {
console.log("Node.js version did not switch successfully. Please run the nvm use command manually.");
} else {
console.log("Updated Node.js version:", updatedVersion);
}
};
const nvmUseVersion = (nvmrcVersion) => {
// Attempt to switch to the desired Node.js version using `nvm use`
const useOutput = execSync(`nvm use ${nvmrcVersion}`, { encoding: "utf-8" }).trim();
if (useOutput.includes("is not installed.")) {
// If the desired version is not installed, check available versions
const versions = execSync("nvm ls", { encoding: "utf-8" });
if (!versions.includes(nvmrcVersion)) {
console.log(`Node.js version ${nvmrcVersion} is not installed.`);
requestToInstallNewVersion(nvmrcVersion);
}
} else {
checkVersionAfterUpdate(nvmrcVersion);
}
};
main();
The text was updated successfully, but these errors were encountered:
proCoderSid
changed the title
[Feature]: NVM automatic version change based on .nvmrc file
[Feature]: NVM automatic version change based on .nvmrc file Using JS Script Available to use
Oct 9, 2023
Since this is a feature I'm not implementing in NVM4W, I'm closing this. However; I'm going to link to this from the wiki for people who want this functionality.
In addition I had to tweak two lines because when comparing values it would fail since some versions have leading "v" before version i.e. 'v18.0.0' === '18.0.0' would fail, though technically they are the same version.
Description of the new feature / enhancement
I have created this script and it is for user convince to go to check script
Automatic Node.js Version Checker Script
This JavaScript script automates the process of checking and managing Node.js versions based on specific conditions:
Check Current Node.js Version:
Comparison with Required Version from
.nvmrc
File:Using Node Version from
.nvmrc
File:.nvmrc
file in your project directory..nvmrc
file is found and contains a valid Node.js version, the script switches to and uses that version.Installing Missing Node.js Version:
.nvmrc
file or the specified version is not installed, the script prompts the user with a confirmation message.nvm install version
commandTerminating the Script:
This script provides an automated and user-friendly way to ensure that your Node.js environment is set to the required version for your project.
Usage
the script is available at https://github.com/proCoderSid/nvm-chk.git
You can use this file in
package.json
file as follow or as you feel comfortable to useI have tested this script in node v4.9.11 to latest,
It works fine and if not working in any specific version then let me know I will update the script
Prerequisites as follow
Scenario when this would be used?
Supporting information
The text was updated successfully, but these errors were encountered: