Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
rtritto opened this issue Jun 28, 2024 · 5 comments
Open
3 tasks done

Comments

@rtritto
Copy link

rtritto commented Jun 28, 2024

Pre-flight checklist

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project uses.
  • 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

Electron version

31.0.1

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
Copy link
Author

rtritto commented Jul 5, 2024

@jdms754 what do you think about this issue?

@jdms754
Copy link

jdms754 commented Jul 6, 2024

@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.

@rtritto
Copy link
Author

rtritto commented Jul 7, 2024

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:

@jdms754
Copy link

jdms754 commented Jul 8, 2024

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:

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.

@rtritto
Copy link
Author

rtritto commented Jul 8, 2024

I used the template created by @caoxiemeihao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants