• 运行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
    
    > 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    原因分析:

    目前我的 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    解决方案:

    采用 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

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

    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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    代码随想录训练营 | 一刷总结
    windows下安装hbase
    CSS 实现将多个卡片分成两列显示
    产业互联网周报:我国数字经济规模超45万亿元;国家网信办就个人信息出境标准公开征求意见;阿里巴巴成立子公司瓴羊;国家发改委……...
    OpenHarmony轻内核编码规范
    java -构造方法
    7.elasticsearch字段类型列表
    JS常用时间操作moment.js参考文档
    网关 GateWay 的使用详解、路由、过滤器、跨域配置
    Vue2和Vue3响应式区别和理解
  • 原文地址:https://blog.csdn.net/to_the_Future/article/details/133638195