实现在容器中项目自动切换node版本
前提
-
Win 11 - 已安装
docker-desktop - 已安装
WSL 2,开启Linux子系统 - 已安装
VSCode - 使用
frankfang128/oh-my-docker:mangosteen- 已安装
nvm - 已安装
zsh
- 已安装
添加 .nvmrc 到项目文件夹中
在需要切换
node版本的项目中新建.nvmrc文件,并写入版本号:
v16.20.2
- 不同项目中可分别写不同的版本
VSCode 安装插件 vsc-nvm
- 安装完插件,打开新的终端即可
手动实现脚本 ⇧
- 考虑到第三方插件不稳定以及安全等因素,可以手动实现脚本
- 将以下脚本添加到
~/.zshrc
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
echo 'now exec load-nvmrc which in ~/.zshrc...'
local nvmrc_path
nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version
nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then
nvm use
fi
elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
实现cd进入项目文件时,指定的node版本
- 之前的步骤已经基本满足需求,还想画蛇添足,打印开发环境的版本
- 以下是用
ai辅助写出的脚本:
自动切换脚本,示例:
switch_node.sh
#!/bin/bash
# Function to switch Node.js version based on .nvmrc file
switch_node_version() {
local nvmrc_file=".nvmrc"
local node_version
if [[ -f "$nvmrc_file" ]]; then
node_version=$(cat "$nvmrc_file")
if [[ -n $node_version ]]; then
echo "nvm switching node version to $node_version ..."
nvm use "$node_version" "$@"
if [ "$?" = "1" ]; then
echo "无法切换版本!" 1>&2
exit 1
fi
else
echo "The node version in .nvmrc is not installed, please run: nvm install lts";
fi
else
echo "no .nvmrc file in current path, use global config...";
fi
}
# 判断是否存在 版本
ifHasVersionPrint() {
local exe="${1:?"exe name missing."}"
local install_info=$2
if command_path=$(command -v $1 2>/dev/null); then
echo "${exe^^} Version: $($1 --version)"
else
echo "${exe^^} is not installed"
if [[ -n $install_info ]]; then
echo "Please run: $install_info"
fi
fi
}
# Automatically switch Node.js version when entering a directory
# 自动切换到项目文件夹中 .nvmrc 配置的 Node.js 版本并打印相关版本信息
cd() {
builtin cd "$@"
if command -v node >/dev/null 2>&1; then
switch_node_version;
if [ "$?" = "0" ]; then
whoami
uname -a
ifHasVersionPrint nvm;
nvm_current=$(nvm current)
echo "nvm current: $nvm_current"
ifHasVersionPrint nrm 'npm install -g nrm';
nrm_current=$(nrm current)
echo "nrm current: $nrm_current"
ifHasVersionPrint npm;
ifHasVersionPrint pnpm 'corepack prepare pnpm@latest --activate';
ifHasVersionPrint yarn 'corepack prepare yarn@1.22.19 --activate';
ifHasVersionPrint ruby;
echo "$(ifHasVersionPrint node)";
fi
else
echo "node is not installed"
fi
}
# Function to print Node.js version in current path
print_env_version() {
# Print separator
echo "-------print_env_version-------"
#Print current user
whoami
#Print current system
uname -a
# Print Node.js version
ifHasVersionPrint node;
# Print nvm version
ifHasVersionPrint nvm;
nvm_current=$(nvm current)
echo "nvm current: $nvm_current"
# Print nrm current
ifHasVersionPrint nrm 'npm install -g nrm';
nrm_current=$(nrm current)
echo "nrm current: $nrm_current"
# Print npm version
ifHasVersionPrint npm;
# Print pnpm version
ifHasVersionPrint pnpm 'corepack prepare pnpm@latest --activate';
# Print yarn version
ifHasVersionPrint yarn 'corepack prepare yarn@1.22.19 --activate';
# Print ruby version
ifHasVersionPrint ruby;
# Print go version
go_version=$(go version)
echo "go version: $go_version"
# Print separator
echo "--------------------------------"
}
#######################################################################
# Automatically switch Node.js version when starting a new Bash session
if [[ -n "$BASH_VERSION" ]]; then
cd .
fi
·未完待续·
参考文章
- 如何限制项目使用指定的node版本
- 锁定项目的 node 版本
- 根据项目的.nvmrc 自动切换node版本
- run
nvm useautomatically every time there's a .nvmrc file on the directory - avn: An Automatic Version Switching for Node.js
- semver 语义化版本 Semantic Versioning
- docker 构建centos7+git+nvm镜像,实现自主切换node版本统一部署前端vue项目
- nvm-一个node版本管理工具 + 打开终端自动切换node版本教程
- nvm安装及使用 + 打开终端自动切换node版本教程
- 作者: Joel
- 文章链接:
- 版权声明
- 非自由转载-非商用-非衍生-保持署名