electron-vue-next icon indicating copy to clipboard operation
electron-vue-next copied to clipboard

引入electron-fetch会导致报错无法运行

Open ifrvn opened this issue 4 years ago • 3 comments

init 项目后安装 electron-fetch ,然后在src/main下引入electron-fetch后运行项目就会报错

复现过程

  1. yarn create electron-vue-next 一路默认,然后进入目录执行yarn
  2. yarn add -D electron-fetch
  3. src/main/index.ts中添加如下两行
    import fetch from 'electron-fetch'
    const theFetch = fetch
    
    执行 yarn dev(如果之前有运行过项目,先删除dist目录,否则会使用之前生成的dist/index.js启动窗口,不会弹出报错窗口)

错误信息

> Executing task: yarn run dev <

yarn run v1.22.5
$ node scripts/dev.js
(node:16840) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./dist/" in the "exports" field module resolution of the package at D:\dev\test\node_modules\rollup\package.json.
Update this package.json to use a subpath pattern like "./dist/*".
(Use `node --trace-deprecation ...` to show where the warning was created)
[DEV] Dev server listening on 3031

  vite v2.3.8 dev server running at:

  > Local: http://localhost:8080/
  > Network: use `--host` to expose
{
  code: 'ERROR',
  error: [Error: Could not load D:\dev\test\node_modules\iconv-lite\lib\index.js.ts (imported by D:\dev\test\node_modules\iconv-lite\lib\index.js?commonjs-proxy): ENOENT: no such file or directory, open 'D:\dev\test\node_modules\iconv-lite\lib\index.js.ts'] {     
    errno: -4058,
    code: 'ENOENT',
    syscall: 'open',
    path: 'D:\\dev\\test\\node_modules\\iconv-lite\\lib\\index.js.ts',
    watchFiles: [
      'D:\\dev\\test\\src\\main\\index.dev.ts',
      'vue-devtools',
      'D:\\dev\\test\\src\\main\\index.ts',
      'D:\\dev\\test\\static/logo.png?static',
      'D:\\dev\\test\\src\\renderer/index.html?renderer',
      'D:\\dev\\test\\src\\renderer/side.html?renderer',
      'D:\\dev\\test\\src\\preload/index?preload',
      'D:\\dev\\test\\src\\preload/another?preload',
      'D:\\dev\\test\\src\\main\\logger.ts',
      'D:\\dev\\test\\src\\main\\dialog.ts',
      'D:\\dev\\test\\src\\main\\services\\index.ts',
      'D:\\dev\\test\\src\\preload\\index.ts',
      'D:\\dev\\test\\src\\preload\\another.ts',
      'D:\\dev\\test\\src\\main\\services\\BaseService.ts',
      'D:\\dev\\test\\src\\main\\services\\Service.ts',
      'D:\\dev\\test\\src\\main\\services\\FooService.ts',
      'D:\\dev\\test\\src\\shared\\sharedLib.ts',
      'D:\\dev\\test\\node_modules\\electron-fetch\\lib\\index.es.js',
      'D:\\dev\\test\\node_modules\\encoding\\lib\\encoding.js',
      '\x00commonjsHelpers.js.ts',
      '\x00D:\\dev\\test\\node_modules\\encoding\\lib\\encoding.js?commonjs-exports',
      'D:\\dev\\test\\node_modules\\iconv-lite\\lib\\index.js',
      '\x00D:\\dev\\test\\node_modules\\iconv-lite\\lib\\index.js?commonjs-proxy',
      '\x00D:\\dev\\test\\node_modules\\iconv-lite\\lib\\index.js?commonjs-module',
      '\x00stream?commonjs-external',
      'D:\\dev\\test\\node_modules\\iconv-lite\\lib\\index.js.ts'
    ]
  },
  result: null
}
[DEV] Electron app started
[DEV] Skip start/reload preload.

[CONSOLE] Debugger listening on ws://127.0.0.1:5858/a5f5bdcd-4ebc-4ced-97fb-e851cc7baee6[CONSOLE] For help, see: https://nodejs.org/en/docs/inspector

image

环境信息

Windows 10 x64 node v16.3.0 yarn 1.22.5 electron-vue-next commit/eeeb5748e7b9756414da304242dbf71ee31f63a8 electron-fetch ^1.7.3

# .yarnrc
registry "https://registry.npm.taobao.org"
electron_mirror "https://npm.taobao.org/mirrors/electron/"

ifrvn avatar Jul 04 '21 02:07 ifrvn

尝试改了下rollup.esbuild.plugin.js,现在不会报错了。但我不太熟rollup,也不知道这么改有没有什么问题...就不发PR了 image

      const dtsResult = await this.resolve(`${id}.d.ts`, importer, { skipSelf: true })
      if (dtsResult) {
        return
      }
      const indexDTsResult = await this.resolve(`${id}/index.d.ts`, importer, { skipSelf: true })
      if (indexDTsResult) {
        return
      }

ifrvn avatar Jul 04 '21 12:07 ifrvn

神奇,其实我也不太会rollup,尤其是resolve ts这块其实挺蛋疼的。。。 我回头研究研究正常的写法……

Update: 我试了 npm 好像没遇到这个问题…… 你试试在esbuild的resolveId这里加这个试试,这样work嘛

    async resolveId(id, importer) {
      if (/\?commonjs/.test(id) || id === 'commonjsHelpers.js' || id.endsWith('js')) {
        return
      }

ci010 avatar Jul 07 '21 08:07 ci010

@ci010 npm一样会报错,rollup的问题跟包管理器没关系。改了之后it works

ifrvn avatar Jul 07 '21 09:07 ifrvn