运行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.
相关推荐
大家的林语冰4 分钟前
前端周刊:axios 疑遭朝鲜黑客“钓鱼“;CSS 新函数上线;npm 上线深色主题;Oxlint 兼容表;ESLint 支持 Temporal......
前端·javascript·css
哀木1 小时前
一个简单的套壳方案,就能让你的 Agent 少做重复初始化
前端
问心无愧05131 小时前
ctf show web入门27
前端
小村儿2 小时前
给 AI Agent 装上"长期记忆":Karpathy 的 LLM Wiki 思想,我做成了工具
前端·后端·ai编程
竹林8182 小时前
用ethers.js连接MetaMask实现Web3钱包登录:从踩坑到稳定运行的完整记录
前端·javascript
heyCHEEMS2 小时前
如何用 Recast 实现静态配置文件源码级读写
前端·node.js
心连欣2 小时前
从零开始,学习所有指令!
前端·javascript·vue.js
review445432 小时前
大模型和function calling分别是如何工作的
前端
东东同学2 小时前
耗时一个月,我把 Nuxt 首屏性能排障经验做成了一个 AI Skill
前端·agent
冴羽3 小时前
超越 Vibe Coding —— AI 辅助编程指南
前端·ai编程·vibecoding