运行vite项目报错:await import(‘source-map-support‘).then((r) => r.default.install())

项目场景:

Electron + vue3 + vite项目实现屏幕截图功能


问题描述

运行 npm run dev 启动项目报错 await import('source-map-support').then((r) => r.default.install())

bash 复制代码
PS D:\study\electron\electronDemo> npm run dev

> electronDemo@0.0.1 dev D:\study\electron\electronDemo> vite --mode development

file:///D:/study/electron/electronDemo/node_modules/vite/bin/vite.js:7
    await import('source-map-support').then((r) => r.default.install())
    ^^^^^

SyntaxError: Unexpected reserved word
    at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
    at async link (internal/modules/esm/module_job.js:37:21)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! electronDemo@0.0.1 dev: `vite --mode development`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the electronDemo@0.0.1 dev 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\AppData\Roaming\npm-cache\_logs\2023-10-07T06_08_23_492Z-debug.log

原因分析:

目前我的 nodeJS 版本是 14.1.0 ,初步怀疑是 nodeJS 版本引起的 vite 报错

bash 复制代码
PS D:\study\electron\electronDemo> nvm ls

    16.19.0
    16.15.0
    14.19.3
    14.18.3
  * 14.1.0 (Currently using 64-bit executable)
    12.19.0
PS D:\study\electron\electronDemo>

解决方案:

采用 nvm 切换管理 nodeJS 版本,关于 nvm 的使用,请自行搜索资料。

bash 复制代码
PS D:\study\electron\electronDemo> nvm ls

    16.19.0
    16.15.0
    14.19.3
    14.18.3
  * 14.1.0 (Currently using 64-bit executable)
    12.19.0
PS D:\study\electron\electronDemo> nvm use 16.19.0
Now using node v16.19.0 (64-bit)
PS D:\study\electron\electronDemo> nvm ls

  * 16.19.0 (Currently using 64-bit executable)
    16.15.0
    14.19.3
    14.18.3
    14.1.0
    12.19.0
PS D:\study\electron\electronDemo>

再次启动项目,成功执行:

bash 复制代码
PS D:\study\electron\electronDemo> npm run dev

> electronDemo@0.0.1 dev
> vite --mode development


  VITE v4.4.9  ready in 1346 ms

  ➜  Local:   http://127.0.0.1:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
vite v4.4.9 building for development...

watching for file changes...

build started...
✓ 3 modules transformed.
dist-electron/preload.js  0.40 kB │ gzip: 0.22 kB
dist-electron/main.js     1.60 kB │ gzip: 0.82 kB
built in 164ms.
相关推荐
FRing032 小时前
vue3干货总结3(defineOptions(Vue3.3)、defineModel(Vue3.4)、Pinia)
开发语言·前端·vue.js·vue3
Bczheng12 小时前
组件化服务端渲染模式SSR
前端
whitelbwwww3 小时前
CUDA C++实战--中级
开发语言·前端·c++
张紫娃4 小时前
Chrome插件-Stylus(屏蔽博客广告)
前端·chrome·stylus
Yannik4 小时前
在 Apple Silicon Mac 上用 Jenkins + Fastlane 搭建 iOS CI:从自动打包到 MR 检查流水线的完整实践
前端
Nayana4 小时前
《Web 到 HarmonyOS》-- 打包:Vite 产物 → HAP / HAR / HSP
前端
inksci4 小时前
用飞帆做了个小网页
前端·javascript·css·html
去伪存真4 小时前
被 pnpm 打包坑惨后,我彻底盘明白了硬链接与软链接
前端·electron
zhensherlock5 小时前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js
tryCbest10 小时前
Vue3——第 22 篇:Pinia——新一代状态管理(上)
前端·javascript·vue.js