运行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())

PS D:\study\electron\electronDemo> npm run dev

> [email protected] 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! [email protected] dev: `vite --mode development`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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 报错

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 的使用,请自行搜索资料。

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>

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

PS D:\study\electron\electronDemo> npm run dev

> [email protected] 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.

你可能感兴趣的:(前端,vite,前端)