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

ESM example/usage #49

Open
rtritto opened this issue Jun 17, 2024 · 1 comment
Open

ESM example/usage #49

rtritto opened this issue Jun 17, 2024 · 1 comment

Comments

@rtritto
Copy link

rtritto commented Jun 17, 2024

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'
    import { fileURLToPath } from 'node:url'
    
    const require = createRequire(import.meta.url)
    const __dirname = path.dirname(fileURLToPath(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: [__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: an example for ESM use case should be added in README.md.

Reproduction

template-electron-vite

@uncaught
Copy link

uncaught commented Jul 6, 2024

I've simply copied the script and converted it to ESM locally:

import path from 'path';
import {spawn} from 'child_process';
import _debug from 'debug';
import {app} from 'electron';

//Copied from https://github.com/mongodb-js/electron-squirrel-startup/blob/master/index.js due to missing ESM support

const debug = _debug('electron-squirrel-startup');

function run(args: string[], done: () => void) {
  const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe');
  debug('Spawning `%s` with args `%s`', updateExe, args);
  spawn(updateExe, args, {
    detached: true,
  }).on('close', done);
}

function check(): boolean {
  if (process.platform === 'win32') {
    const cmd = process.argv[1];
    debug('processing squirrel command `%s`', cmd);
    const target = path.basename(process.execPath);

    if (cmd === '--squirrel-install' || cmd === '--squirrel-updated') {
      run(['--createShortcut=' + target + ''], app.quit);
      return true;
    }
    if (cmd === '--squirrel-uninstall') {
      run(['--removeShortcut=' + target + ''], app.quit);
      return true;
    }
    if (cmd === '--squirrel-obsolete') {
      app.quit();
      return true;
    }
  }
  return false;
}

export default check();

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

No branches or pull requests

2 participants