forge icon indicating copy to clipboard operation
forge copied to clipboard

[ERR_MODULE_NOT_FOUND] [ESM] Vite doesn't bundle internal dependencies

Open rtritto opened this issue 1 year ago • 7 comments

Pre-flight checklist

  • [X] I have read the contribution documentation for this project.
  • [X] I agree to follow the code of conduct that this project uses.
  • [X] I have searched the issue tracker for a bug that matches the one I want to file, without success.

Electron Forge version

7.4.0 or 7.5.0

Electron version

31.0.1 or 32.1.2

Operating system

Windows 11

Last known working Electron Forge version

No response

Expected behavior

No error in console

Actual behavior

With ESM, the electron-squirrel-startup dependency can't be found if it's in dependencies (it works if I move it into devDependencies):

  • require.resolve (Node < 20.6.0) example; src/main.ts file:

    import { createRequire } from 'node:module'
    
    const require = createRequire(import.meta.url)
    
    try {
      // Handle creating/removing shortcuts on Windows when installing/uninstalling.
      require.resolve('electron-squirrel-startup/package.json')
      // OR require.resolve('electron-squirrel-startup/package.json', { paths: [import.meta.dirname] })
      app.quit()
    } catch (error) {
      // Ignore
      console.log('Ignore error: ', error)
    }
    

    Console output:

      Ignore error:  Error: Cannot find module 'electron-squirrel-startup/package.json'
      Require stack:
      - C:\template-electron-vite\.vite\build\main.js
          at Module._resolveFilename (node:internal/modules/cjs/loader:1152:15)
          at s._resolveFilename (node:electron/js2c/browser_init:2:120699)
          at Function.resolve (node:internal/modules/helpers:190:19)
          at file:///C:/template-electron-vite/.vite/build/main.js:8:12
          at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
          at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
          at async loadApplicationPackage (file:///C:/template-electron-vite/.yarn/unplugged/electron-npm-31.0.0-df97707901/node_modules/electron/dist/resources/default_app.asar/main.js:129:9)
          at async file:///C:/template-electron-vite/.yarn/unplugged/electron-npm-31.0.0-df97707901/node_modules/electron/dist/resources/default_app.asar/main.js:241:9 {    
        code: 'MODULE_NOT_FOUND',
        requireStack: [ 'C:\\template-electron-vite\\.vite\\build\\main.js' ]
      }
    
  • import.meta.resolve (Node.js >= 20.6.0) example; src/main.ts file:

    try {
      // Handle creating/removing shortcuts on Windows when installing/uninstalling.
      import.meta.resolve('electron-squirrel-startup/package.json')
      app.quit()
    } catch (error) {
      // Ignore
      console.log('Ignore error: ', error)
    }
    

    Console output:

    Ignore error:  Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'electron-squirrel-startup' imported from C:\template-electron-vite\.vite\build\main.js
        at packageResolve (node:internal/modules/esm/resolve:861:9)
        at moduleResolve (node:internal/modules/esm/resolve:934:18)
        at defaultResolve (node:internal/modules/esm/resolve:1164:11)
        at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
        at ModuleLoader.resolveSync (node:internal/modules/esm/loader:365:17)
        at Object.resolve (node:internal/modules/esm/initialize_import_meta:30:25)
        at file:///C:/template-electron-vite/.vite/build/main.js:6:15
        at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
        at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
        at async loadApplicationPackage (file:///C:/template-electron-vite/.yarn/unplugged/electron-npm-31.0.0-df97707901/node_modules/electron/dist/resources/default_app.asar/main.js:129:9) {
      code: 'ERR_MODULE_NOT_FOUND'
    }
    

Note: I did a try with other dependencies such as react and solid-js and I got same issue.

Steps to reproduce

Repository: https://github.com/rtritto/template-electron-vite

Steps:

  • git clone https://github.com/rtritto/template-electron-vite.git
  • cd template-electron-vite
  • yarn
  • yarn start

Additional information

No response

rtritto avatar Jun 28 '24 14:06 rtritto

@jdms754 what do you think about this issue?

rtritto avatar Jul 05 '24 23:07 rtritto

@jdms754 what do you think about this issue?

I added import.meta.resolve('electron-squirrel-startup/package.json') anywhere in https://github.com/jdms754/forge-vite-bundler/blob/main/src/main.ts and was able to npm run start. I am on macOS Sonoma 14.5, but I am not sure that is the cause of the difference.

jdms754 avatar Jul 06 '24 00:07 jdms754

I've already tried with import.meta.resolve('electron-squirrel-startup/package.json') (issue description). Maybe it's related to some settings in tsconfig.json:

  • https://github.com/rtritto/template-electron-vite/blob/master/tsconfig.json
  • https://github.com/caoxiemeihao/electron-vite-samples/blob/main/forge-esm-ts/tsconfig.json
  • https://github.com/jdms754/forge-vite-bundler/blob/main/tsconfig.json

rtritto avatar Jul 07 '24 10:07 rtritto

I've already tried with import.meta.resolve('electron-squirrel-startup/package.json') (issue description). Maybe it's related to some settings in tsconfig.json:

  • https://github.com/rtritto/template-electron-vite/blob/master/tsconfig.json
  • https://github.com/caoxiemeihao/electron-vite-samples/blob/main/forge-esm-ts/tsconfig.json
  • https://github.com/jdms754/forge-vite-bundler/blob/main/tsconfig.json

Yes, looks like it's tsconfig.json. Why not set moduleResolution to node16 or bundler? You will also need to update module in the same file and you may have to make additional changes to your vite config files.

jdms754 avatar Jul 08 '24 04:07 jdms754

I used the template created by @caoxiemeihao

rtritto avatar Jul 08 '24 14:07 rtritto

@caoxiemeihao should external constants in /packages/plugin/vite/src/config/vite.base.config.ts also include all dependencies in package.json?

Related code: https://github.com/electron/forge/blob/420458bd2e6f34e4298c3a8d028c26e6e43ff90e/packages/plugin/vite/src/config/vite.base.config.ts#L8

E.g. (draft):

+ import pkg from './package.json'

- export const external = [...builtins];
+ export const external = [...builtins, ...Object.keys(pkg.dependencies)]; 

FYI @erickzhao @MarshallOfSound

rtritto avatar Sep 21 '24 13:09 rtritto

Maybe only dev dependencies should be exist (no dependencies) because there is the build step.

rtritto avatar Sep 29 '24 13:09 rtritto

Is this fixed in particular by https://github.com/electron/forge/pull/3713?

erickzhao avatar Jan 31 '25 18:01 erickzhao

No, same issue with [email protected].

Using import.meta.resolve('electron-squirrel-startup/package.json'):

  Ignore error:  Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'electron-squirrel-startup' imported from C:\template-electron-vite\.vite\build\main.js
    at packageResolve (node:internal/modules/esm/resolve:861:9)
    at moduleResolve (node:internal/modules/esm/resolve:934:18)
    at defaultResolve (node:internal/modules/esm/resolve:1176:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:540:12)
    at ModuleLoader.resolveSync (node:internal/modules/esm/loader:522:17)
    at Object.resolve (node:internal/modules/esm/initialize_import_meta:30:25)
    at file:///C:/template-electron-vite/.vite/build/main.js:66321:15
    at ModuleJob.run (node:internal/modules/esm/module_job:234:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:473:24)
    at async loadApplicationPackage (file:///C:/template-electron-vite/.yarn/unplugged/electron-npm-32.1.2-484029ad69/node_modules/electron/dist/resources/default_app.asar/main.js:129:9) {
  code: 'ERR_MODULE_NOT_FOUND'
}

rtritto avatar Feb 01 '25 01:02 rtritto