使用nvm切换Node版本后仍无法创建Strapi项目求助
问题描述
我在电商项目的api目录下创建Strapi项目时,遇到了Node版本不兼容的问题:明明用nvm切换到了v20.0.0(nvm也确认当前版本正确,且未安装v21.2.0),但执行npx create-strapi-app@latest .时,系统仍提示我运行的是Node.js 21.2.0,不符合Strapi要求的>=18.0.0 <=20.x.x版本范围。
命令行输出如下:
current@0.1.6 PS C:\Users\liamt\Desktop\ecommerce-app\api> nvm current v20.11.0 PS C:\Users\liamt\Desktop\ecommerce-app\api> nvm ls 20.9.0 20.5.0 20.0.0 PS C:\Users\liamt\Desktop\ecommerce-app\api> nvm use 20.0.0 Now using node v20.0.0 (64-bit) PS C:\Users\liamt\Desktop\ecommerce-app\api> npx create-strapi-app@latest . (node:14736) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternatiternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) ? Choose your installation type Custom (manual settings) You are running Node.js 21.2.0 Strapi requires Node.js >=18.0.0 <=20.x.x Please make sure to use the right version of Node. PS C:\Users\liamt\Desktop\ecommerce-app\api>
解决办法
以下是几个可尝试的排查和修复步骤:
- 关闭并重新打开终端:nvm的版本切换可能不会立即在当前终端会话生效,尤其是Windows的PowerShell/CMD,关闭后重新打开,再用
node -v确认版本。 - 检查全局Node安装:可能你之前直接安装过Node(非nvm渠道),导致npx调用了全局的Node版本。执行
where node(Windows)或which node(Mac/Linux),如果输出中有非nvm目录下的Node路径,先卸载全局安装的Node,再重新用nvm切换版本。 - 清除npx缓存:npx可能缓存了旧的Node版本关联,执行
npx clear-npx-cache清除缓存后,再重新运行Strapi创建命令。 - 指定Node版本运行npx:直接用nvm指定的Node来运行npx,比如执行
nvm exec 20.0.0 npx create-strapi-app@latest .,强制使用指定版本的Node执行命令。 - 检查PowerShell执行策略(Windows):有时候PowerShell的执行策略会影响nvm的环境变量加载,尝试以管理员身份打开PowerShell,执行
Set-ExecutionPolicy RemoteSigned(按提示确认),然后重新切换版本。
内容的提问来源于stack exchange,提问作者Liam McWilliams
相关产品推荐
相关产品推荐

