运行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.
相关推荐
zlpzlpzyd39 分钟前
vue.js 3中全局组件和局部组件的区别
前端·javascript·vue.js
浩星1 小时前
css实现类似element官网的磨砂屏幕效果
前端·javascript·css
一只小风华~1 小时前
Vue.js 核心知识点全面解析
前端·javascript·vue.js
2022.11.7始学前端1 小时前
n8n第七节 只提醒重要的待办
前端·javascript·ui·n8n
SakuraOnTheWay1 小时前
React Grab实践 | 记一次与Cursor的有趣对话
前端·cursor
阿星AI工作室1 小时前
gemini3手势互动圣诞树保姆级教程来了!附提示词
前端·人工智能
徐小夕1 小时前
知识库创业复盘:从闭源到开源,这3个教训价值百万
前端·javascript·github
xhxxx1 小时前
函数执行完就销毁?那闭包里的变量凭什么活下来!—— 深入 JS 内存模型
前端·javascript·ecmascript 6
StarkCoder1 小时前
求求你试试 DiffableDataSource!别再手算 indexPath 了(否则迟早崩)
前端
fxshy1 小时前
Cursor 前端Global Cursor Rules
前端·cursor