Spotted your comment about using a vscode plugin
If all your looking for is to change your nvm version for all terminals when the version differes on one of your projects then check out my overkill answer.
I will probably put all of the logic in a bash script and just share the link. 👍
Usually version of node is stored in the package.json and causing an incorrect version of node or npm to cause a failure.
// package.json
"engines": {
"node": ">=10.0.0"
},
"scripts": {
Usually version logic is in bashrc sourced by the shell in the $HOME/.bashrc file.
when new window windows are created they re-trigger nvm use.
Two possibilities:
both use script: script-to-replace-nvm-version.sh
# Which replaces in bashrc the "nvm use " line with "nvm use $correctProjectVersion"
Use start script in package json to run script-to-replace-nvm-version
Use PROMPT_COMMAND to run script-to-replace-nvm-version:
a check is used here to make sure we are on an npm project.
be careful with PROMPT_COMMAND as it will run every time before the ps1 renders.
PROMPT_COMMAND="[ -f ./package.json ] && $HOME/.scriptsourcedbybashrc.sh"
Script Summary: Nvm is usually sourced with the particular version in $HOME/.bashrc. Therefore we could the npm package rewrite the line containing nvm use ... and update the version. We could even prompt the user with do you wish to update?
check node version from package.json
projectVersion=$(cat ./package.json | grep 'node":' | tr -dc '0-9.')
echo $projectVersion
check node version from $HOME/.bashrc
nodeVersion=$(cat $HOME/.bashrc | grep 'nvm use' | tr -dc '0-9.')
echo $nodeVersion
check if node and package.json versions differ
# bash script, to be sourced by npm run start;
# WIP, needs a few more error checks
# such as, nvm used in more than one place in your bashrc.
if
versions_differ_tell_and_prompt
fi
versions_differ_tell_and_prompt() {
echo "nvm versions differ: ";
found in project: $projectVersion;
found in '$HOME/.bashrc': $nodeVersion;
echo ;
echo "ignore with any keypress or [uU] to update";
old_stty_cfg=$(stty -g);
stty raw -echo ; answer=$(head -c 1) ; stty $old_stty_cfg;
}
if echo "$answer" | grep -iq "^u" ;then
update_file 'nvm use' "nvm use $projectVersion" && source $HOME.bashrc;
else
echo "No changes made";
fi
update_file() {
searchLine=$1
replacementLine=$2
lineNumber=$(cat $HOME/.bashrc | grep -n $searchLine | cut -f1 -d:)
cp "$HOME/.bashrc" "$HOME/.bashrc.backup"
sed -i "$lineNumbers/.*/$replacementLine/" "$HOME/.bashrc"
source "$HOME/.bashrc"
}
unset searchLine
unset replacementLine
unset answer
unset old_stty_cfg
unset nodeVersion
unset projectVersion
Some other helpful links:
https://github.com/md-command-line/ERRORSCREAM/issues/2
took most of the logic from here: https://github.com/MichaelDimmitt/git_check_computer/blob/master/git_check_computer.sh
correct the yarn start command to run the correct yarn or npm based on the lockfile found.
https://github.com/MichaelDimmitt/know-your-package-manager
nvm use
automatically. I ended up using a plugin for vscode for nvm, so it simply runsnvm use
in my project folder in built-in terminal, so whatever project is opened - nvm switches to proper nodejs automatically. Not an ideal solution, because it’s only applied to vscode, but alright[ "$(node -v)" != "v10.24.1" ] && source run-nvm.sh && yarn start || yarn start
bash ./start
, also you could grep from package.json to get the node version. Instead of hardcoding like I did.