You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{createRequire}from'node:module'import{fileURLToPath}from'node:url'constrequire=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){// Ignoreconsole.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' ] }
try{// Handle creating/removing shortcuts on Windows when installing/uninstalling.import.meta.resolve('electron-squirrel-startup/package.json')app.quit()}catch(error){// Ignoreconsole.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.
I've simply copied the script and converted it to ESM locally:
importpathfrom'path';import{spawn}from'child_process';import_debugfrom'debug';import{app}from'electron';//Copied from https://github.com/mongodb-js/electron-squirrel-startup/blob/master/index.js due to missing ESM supportconstdebug=_debug('electron-squirrel-startup');functionrun(args: string[],done: ()=>void){constupdateExe=path.resolve(path.dirname(process.execPath),'..','Update.exe');debug('Spawning `%s` with args `%s`',updateExe,args);spawn(updateExe,args,{detached: true,}).on('close',done);}functioncheck(): boolean{if(process.platform==='win32'){constcmd=process.argv[1];debug('processing squirrel command `%s`',cmd);consttarget=path.basename(process.execPath);if(cmd==='--squirrel-install'||cmd==='--squirrel-updated'){run(['--createShortcut='+target+''],app.quit);returntrue;}if(cmd==='--squirrel-uninstall'){run(['--removeShortcut='+target+''],app.quit);returntrue;}if(cmd==='--squirrel-obsolete'){app.quit();returntrue;}}returnfalse;}exportdefaultcheck();
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:Console output:
import.meta.resolve
(Node.js >= 20.6.0) example;src/main.ts
file:Console output:
Note: an example for ESM use case should be added in README.md.
Reproduction
template-electron-vite
The text was updated successfully, but these errors were encountered: