这里记录个问题:
1.按照vite官网的建议新建vite项目执行npm run build却报错?
2.问题描述
我新建的vite+vue3+ts项目,执行npm run dev 运行正常,执行 npm run build直接报错,很明显是node版本不对。
当前运行环境如下,可以看到其实是满足官网提到的node 14.18+:
执行npm run build报错如下:
D:\gitee\vite-project>npm run build
> vite-project@0.0.0 build D:\gitee\vite-project
> vue-tsc && vite build
D:\gitee\vite-project\node_modules\@volar\vue-language-core\out\generators\template.js:203
tagOffsetsMap[tag] ??= [];
^^^
SyntaxError: Unexpected token '??='
at wrapSafe (internal/modules/cjs/loader.js:1001:16)
at Module._compile (internal/modules/cjs/loader.js:1049:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:101:18)
at Object.<anonymous> (D:\gitee\vite-project\node_modules\@volar\vue-language-core\out\index.js:18:14)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! vite-project@0.0.0 build: `vue-tsc && vite build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the vite-project@0.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2023-05-09T01_30_14_954Z-debug.log
3.解决办法,还是升级node版本:
升级node版本为16.14.2+,然后删除原来的node_modules文件夹和package-lock.json文件,重新安装依赖即可打包成功。
若对你有帮助,请点个赞吧,谢谢支持!
参考:
vite官网