vue使用electron打包后的问题

vue使用electron打包成桌面应用后启动项目报 Cannot use import statement outside a module
咋处理 百度看了是因为es6语法的问题 我配置了type=module报出了另外一个错

在Electron中,要使用import语句需要在主进程(main process)和渲染进程(renderer process)中分别设置nodeIntegration选项为true。

在主进程中,在创建BrowserWindow实例的时候需要将nodeIntegration设置为true:

const win = new BrowserWindow({
  webPreferences: {
    nodeIntegration: true
  }
})

在渲染进程中,可以通过在vue.config.js文件中设置以下配置来启用nodeIntegration:

module.exports = {
  pluginOptions: {
    electronBuilder: {
      builderOptions: {
        nodeIntegration: true
      }
    }
  }
}

如果因为设置了type=module出现了问题,则需要在运行electron程序时添加--experimental-modules选项:

electron --experimental-modules .

或者在package.json文件中添加以下代码:

json
{
  "scripts": {
    "start": "electron --experimental-modules ."
  }
}

这样就可以在Electron中使用ES6模块语法了。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^