用于自动切换项目中适合的node 版本

v1.0.0

根据 package.json 中的 engines 字段自动检测 Node.js 版本要求,如未安装 nvm 则自动安装,并切换到正确的 Node 版本。支持 Windows、macOS 和 Linux。在开发 Node.js 项目、切换 Node 版本或设置开发环境时使用。

0· 68·0 current·0 all-time
Security Scan
VirusTotalVirusTotal
Pending
View report →
OpenClawOpenClaw
Benign
medium confidence
Purpose & Capability
The name/description describe auto-switching Node versions based on package.json and the provided Bash script implements exactly that: reading package.json, detecting required Node version, installing/loading nvm if missing, and installing/using a matching Node. No unrelated credentials, binaries, or config paths are requested.
Instruction Scope
The SKILL.md and the shell script stay within scope (detect project path, read package.json, install/use nvm, install Node versions). Notes: (1) SKILL.md references a PowerShell script (nvm-auto-switch.ps1) for Windows but that file is not included — missing file means Windows behavior is unspecified; (2) package.json is parsed with a simple grep which is brittle for non-standard formatting; (3) the script performs a network install of nvm by piping the remote install script to bash—this is expected for auto-install but is an important execution-time risk to be aware of.
Install Mechanism
There is no separate install spec (instruction-only with an included script). The script runs curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash to install nvm. Using the official nvm raw URL and a pinned version is reasonable, but curl|bash executes remote code at run-time and carries the usual remote-execution risk (supply-chain/tampering).
Credentials
The skill declares no environment variables or credentials and does not request unrelated access. The script uses standard environment values (HOME, PATH) and external tools (curl, node, npm, nvm) appropriate to its function. SKILL.md correctly notes that Windows may require admin rights and macOS/Linux require curl.
Persistence & Privilege
The skill is not force-included (always: false) and doesn't attempt to modify other skills or system-wide settings. It simply installs/loads nvm in the user's home environment when run.
Assessment
This skill is internally consistent with its purpose, but review these points before using it: 1) The Bash script will download and immediately execute the nvm install script from GitHub (curl | bash). Although it uses the official nvm raw URL and a pinned tag, executing remote installers has supply-chain risk — if you are cautious, install nvm manually or inspect the install script before running. 2) The SKILL.md mentions a Windows PowerShell script but that file is not included; Windows behavior is therefore unclear. 3) The package.json parsing is simple and may fail on unusual formatting; test in a safe project first. 4) Run this as a non-root user, and be prepared to restart your shell after nvm installation. If you need higher assurance, fetch the install script, verify its contents (or checksum), and run it interactively rather than piping it directly to bash.

Like a lobster shell, security has layers — review code before you run it.

latestvk976kcf8dd0xszts3m2bygw1n184db2p
68downloads
0stars
1versions
Updated 1w ago
v1.0.0
MIT-0

NVM 自动切换

根据 package.json 的 engines 字段自动切换到正确的 Node.js 版本。

使用场景

  • 进入 Node.js 项目目录
  • 设置新的开发环境
  • Node 版本不匹配错误
  • CI/CD 流水线设置

快速开始

Windows

# 自动获取当前目录
.\scripts\nvm-auto-switch.ps1

# 或指定项目路径
.\scripts\nvm-auto-switch.ps1 [项目路径]

macOS / Linux

# 自动获取当前目录
./scripts/nvm-auto-switch.sh

# 或指定项目路径
./scripts/nvm-auto-switch.sh [项目路径]

工作原理

  1. 检测操作系统 - 识别 Windows、macOS 或 Linux
  2. 自动获取项目路径 - 如未提供路径,自动使用当前工作目录
  3. 检查当前 Node 版本 - 获取当前激活的 Node.js 版本
  4. 检查/安装 NVM - 如未安装则自动安装
  5. 读取 package.json - 解析 engines.node 字段
  6. 对比版本 - 如果当前版本已满足要求,跳过切换
  7. 查找/安装 Node - 使用现有版本或下载匹配版本
  8. 切换版本 - 激活正确的 Node 版本

工作流程

获取项目路径(参数或当前目录)
↓
检查当前 Node 版本
↓
读取 package.json engines.node
解析版本要求 (例如 >=14.0.0)
↓
当前版本已满足要求?
├── 是 → 跳过切换,提示当前版本兼容
│
└── 否 → 继续
    ↓
    检查 NVM 是否已安装?
    ├── 否 → 自动安装 NVM
    │   ├── Windows: 下载 nvm-setup.exe
    │   └── macOS/Linux: 运行安装脚本
    │
    └── 是 → 继续
        ↓
        查找匹配的已安装版本?
        ├── 是 → nvm use <版本号>
        │
        └── 否 → nvm install <最新匹配版本>
            nvm use <版本号>

脚本

scripts/ 目录下的工具脚本:

脚本平台用途
nvm-auto-switch.ps1WindowsPowerShell 实现
nvm-auto-switch.shmacOS/LinuxBash 实现

示例

当前目录(自动获取):

./scripts/nvm-auto-switch.sh
.\scripts\nvm-auto-switch.ps1

指定项目:

./scripts/nvm-auto-switch.sh /path/to/project
.\scripts\nvm-auto-switch.ps1 "C:\projects\my-app"

注意事项

  • Windows 需要管理员权限来安装 nvm
  • macOS/Linux 需要 curl 来安装 nvm
  • 支持版本前缀:^, ~, >=, <=
  • 安装 nvm 后可能需要重启终端
  • 不提供路径参数时,自动使用当前工作目录

Comments

Loading comments...