Skip to content

[Help] __dirname is not defined in ES module scope #531

@hucoooo

Description

@hucoooo

使用electron-vite-vue时,刚开始正常,当我在main.ts引入puppeteer、Tesseract等插件后,报错:
Could not resolve "bufferutil" imported by "ws". Is it installed?
然后我在vite.config.ts中设置:

main: {
  // Shortcut of `build.lib.entry`.
  entry: 'electron/main.ts',
  vite: {
    build: {
      rollupOptions: {
        external: [
          'bufferutil',
          'utf-8-validate',
          // 其他需要排除的模块
        ]
      }
    },
  },
},

再次运行后就报错:

App threw an error during load
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/mac/wwwroot/hc/my-electron-vite/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions