使用npm init vuetify创建的Vuetify项目运行npm run dev报错
解决Vite+Vuetify3项目npm run dev时的SyntaxError错误
问题场景
使用npm init vuetify创建Vite+Vuetify3项目后,执行npm run dev抛出如下语法错误:
p1@0.0.0 dev > node --no-warnings ./node_modules/.bin/vite C:\vue\p1\node_modules\.bin\vite:2 basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") ^^^^^^^ SyntaxError: missing ) after argument list at internalCompileFunction (node:internal/vm:77:18) at wrapSafe (node:internal/modules/cjs/loader:1288:20) at Module._compile (node:internal/modules/cjs/loader:1340:27) at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) at node:internal/main/run_main_module:28:49
项目创建配置:
√ Project name: ... p1 √ Which preset would you like to install? » Base (Default, Routing) √ Use TypeScript? ... No √ Would you like to install dependencies with yarn, npm, pnpm, or bun? » npm
已尝试更新Node.js、Vue、Vuetify,更换CMD、PowerShell、Git Bash终端,单独使用Vite/VueCLI或VueCLI+Vuetify均正常,问题仍存在。
错误原因
Windows环境下,Node错误执行了Vite的Shell脚本(.sh)而非Windows专用的.cmd脚本,导致Shell语法被Node解析,触发语法错误。
解决方案
1. 修正npm的Shell配置
执行以下命令查看当前npm使用的Shell:
npm config get shell
若输出为Git Bash或其他类Unix Shell,将其改为Windows原生CMD:
npm config set shell "C:\\Windows\\system32\\cmd.exe"
2. 修改package.json的dev脚本
将package.json中的dev脚本修改为直接调用Vite,或指定Windows专用的.cmd文件:
{ "scripts": { "dev": "vite" // 或 "dev": "./node_modules/.bin/vite.cmd" } }
3. 清理依赖重新安装
删除项目根目录下的node_modules文件夹和package-lock.json文件,然后重新安装依赖:
# Windows CMD执行 rmdir /s/q node_modules && del package-lock.json npm install
4. 检查环境变量
确保系统环境变量中,Node.js和npm的路径优先级高于Git等类Unix工具的路径,避免Shell脚本被优先调用。
内容的提问来源于stack exchange,提问作者Joshua H
相关产品推荐
相关产品推荐

