sindresorhus / electron-util Goto Github PK
View Code? Open in Web Editor NEWUseful utilities for Electron apps and modules
License: MIT License
Useful utilities for Electron apps and modules
License: MIT License
This module has some pretty handy tricks that would be generally useful for all Electron apps.
@sindresorhus did you attempt to land any of this stuff in Electron core?
If so, why didn't it work out? If not, why not?
Removed in Electron 9.x: webFrame.setLayoutZoomLevelLimits()
https://www.electronjs.org/docs/breaking-changes#removed-webframesetlayoutzoomlevellimits
At least the Privacy_Camera permissions is missing in the documentation, see https://www.mbsplugins.de/archive/2020-04-05/MacOS_System_Preference_Links
Hi, I am using you utils (on Windows) and here are my comments:
Thanks!
Aperture is using fixPathForAsarUnpack, but when running aperture inside node -p or -c
you will get the following error:
❯ node -p "const recorder = require('aperture')(); recorder.startRecording().then(() => { console.log('started'); setTimeout(() => recorder.stopRecording().then(console.log), 5000) })"
/private/tmp/node_modules/aperture/index.js:16
const isUsingAsar = process.mainModule.filename.includes('app.asar');
TypeError: Cannot read property 'filename' of undefined
at Object.<anonymous> (/private/tmp/node_modules/aperture/index.js:16:40)
Should we add a check that it's run in a file as well?
It seems that Windows does not like a file being named "node.js". I get the below error when trying to run npm start
for the example app on Windows. Simply renaming this file to "nodenew.js" in my testing allowed the example app to be built.
The error from "Windows Script Host":
Script: .\electron-util\node.js
Line: 9
Char: 38
Error: Syntax error
Code: 800A03EA
Source: Microsoft JScript compilation error
The line in question is a valid line, but the error is thrown at the =>
operator:
exports.fixPathForAsarUnpack = path => exports.isUsingAsar ? path.replace('app.asar', 'app.asar.unpacked') : path;
Currently there's openSystemPreferences
, what if there was a readSystemPreferences
or something similar. That way we could see what system preferences were currently set.
Something like:
openSystemPreferences('security', 'Privacy', Screen Recording')
is-using-asar uses process.mainModule
which is deprecated as of v14 and returns undefined
.
This breaks is-using-asar which always returns false
module.exports = isElectron && process.mainModule && process.mainModule.filename.includes('app.asar');
I tried to use mac-screen-capture-permissions
for my Electron app, and that package internally uses electron-util
. When I compile it with TypeScript I get that error:
node_modules/electron-util/index.d.ts:4:9 - error TS2305: Module '"electron"' has no exported member 'AllElectron'.
4 import {AllElectron, Remote, BrowserWindow, Size, Rectangle, Session, MenuItemConstructorOptions, MenuItem} from 'electron';
~~~~~~~~~~~
I added node_modules
to my exclude in tsconfig, but the error is still here. It seems like in new versions of Electron AllElectron
does no longer exist.
Hi, I use this library on Electron v18.
It not work because 'electron-is-dev' version is low, it is not compatible with this version.
electron.remote was deprecated on Electron v14.
I think this issue will resolved if upgrade 'electron-is-dev' version to v2.0.0.
Has any way to solve or avoid this issue?
You make fantastic libraries. Would you recommend this one for use in a production node app, or would you use this only for development?
I only ask because of the line in your readme:
Useful utilities for developing Electron apps...
Cheers!
Implement Electron app with Angular, Typescript, built project and got error import "AllElectron"
ERROR in node_modules/electron-util/index.d.ts:4:9 - error TS2305: Module '"electron"' has no exported member 'AllElectron'.
4 import {AllElectron, Remote, BrowserWindow, Size, Rectangle, Session, MenuItemConstructorOptions, MenuItem} from 'electron';
Please help me to fix this issue. Thanks.
The 'is.development' variable uses the 'electron-is-dev' package, but this package 'must be used from the Electron main process'.
As a result, I won't be able to use 'electron-util' in my renderer process.
Electron 27:
No problem with v0.17.0, not sure what the issue is.
You can reproduce by:
npm i
works as expected.npm i electron-util
npm i
fails with the following:> cross-env NODE_ENV=development TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.renderer.dev.dll.ts
ERROR in dll renderer renderer[26]
Module not found: Error: Exports field key should be relative path and start with "." (key: "main")
From Breaking Changes:
The
remote
module is deprecated in Electron 12, and will be removed in Electron 14. It is replaced by the@electron/remote
module.
This package uses the built in remote
module and also exposes it as api
.
To be able to use it in the coming electron versions it should be replaced with @electron/remote
.
I guess the main problem is that it needs to be initialized in the main process.
So electron-utils
also needs to expose a initialize
function?
An alternative would be to just not use the remote
module anymore, but I guess that would be a big breaking change.
Happy to provide a PR if it is clear how the initialize
step should work or if the remote module should be dropped completely.
I am getting the Following Error when i import:
import { openSystemPreferences } from 'electron-util';
index.js:7 Uncaught TypeError: fs.existsSync is not a function
at getElectronPath (index.js:7)
webPreferences: {
nodeIntegration: true,
allowRunningInsecureContent: serve ? true : false,
contextIsolation: false,
enableRemoteModule: true,
},
Hello,
I'm using fixPathForAsarUnpack
in a function provided to the renderer process in preload. It seems that require.main
is set to undefined
which causes the function to fail.
https://github.com/sindresorhus/electron-util/blob/main/source/is-using-asar.js#L5
Is it not possible to check asar in preload?
Thanks,
Brendan
using vue-cli
and vue-cli-plugin-electron-builder
to mange and build my app.
error occurs when running yarn test:unit
# yarn test:unit
yarn run v1.19.0
$ vue-cli-service test:unit
FAIL tests/unit/refUtils.spec.ts
● Test suite failed to runTypeError: Cannot create proxy with a non-object as target or handler
1 | import WorkbenchTab from '@/components/Workbench/workbenchTab';
2 | import { remote } from 'electron';
> 3 | import { is } from 'electron-util';
| ^
4 |
5 | import { SharedObjectKey } from '@/sharedObjects/sharedObjectKey';
6 |at Object.<anonymous> (node_modules/electron-util/source/api.js:4:18) at Object.<anonymous> (node_modules/electron-util/index.js:8:13) at Object.<anonymous> (src/sharedObjects/sharedObjects.ts:3:1) at Object.<anonymous> (src/logger/index.ts:3:1) at Object.<anonymous> (src/commons/lang/array.ts:1:1) at Object.<anonymous> (src/bindings/types.ts:3:1) at Object.<anonymous> (tests/unit/refUtils.spec.ts:1:1)
electron-util: 0.13.0
Currently if the dock is present the application is centered relative to the available space minus the dock height.
I would like to center my window relative to the total screen space. If I hide the dock or put it on another display the function works as expected.
I am getting an error after upgrading electron-util to 0.14.2 with Electron 11.2.3. Typescript version is 3.3.
node_modules/electron-util/index.d.ts:513:46 - error TS1005: ',' expected.
513 export function appMenu(menuItems?: readonly MenuItemConstructorOptions[]): MenuItemConstructorOptions;
Windows currently supports Dark Mode. Although it is not supported by Electron yet, it seems possible (according to https://stackoverflow.com/a/51336913/3737308) to detect it.
Can it be added to electron-util
? 😅
Hi.
I can't use this library with Next.js. I see this error:
Module not found: Can't resolve 'fs' in '.../node_modules/electron'
Is there any workaround?
Thanks!
There is something changed at 12.x. For example, the remote
module.
Is there a plan for it?
I'm not sure why but this is not found in renderer process. I'm using 0.14.2
with electron 11.3.0
and node 14.5.4
. I've tried it with a fresh project too and no joy.
I get the usual Uncaught TypeError: fs.existsSync is not a function
with const {is} = require('electron-util')
.
Is this mismatched versions or an issue with my use.
EDIT: This also happens when using the included electron-is-dev
. Using standalone [email protected]
works fine so long as electron-util
isn't installed. If electron-util
is installed then const {isDev} = require('electron-is-dev')
defaults to the included [email protected]
and produces the same error.
const aboutData = {
icon: path.join(__dirname, 'build/icons/icon.png');,
title: packageJson.productName,
copyright: `Copyright ${year} ${packageJson.author.name}`,
website: packageJson.author.url,
text: packageJson.description,
}
click() {
showAboutWindow(aboutData);
}
This exact code works on windows and mac but not on linux:
AppImage ( ubuntu / pop_os ) and dep package
electron 5.0.7
electron-builder 21.1.3
Can the enforceMacOSAppLocation
function be published as a standalone module?
That's the only thing provided by this library that I'm using and I'd like to minimize the amount of extra code that is bundled along with my app.
Error when using Typescript
node_modules/electron-util/index.d.ts:4:9 - error TS2305: Module '"electron"' has no exported member 'AllElectron'.
Typescript Version: 4.1.3
Node Version: v14.15.1
Windows: 10.0.19042 Build 19042
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"strictNullChecks": false,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
},
"plugins": [],
},
"include": [
"src/**/*"
]
}
{
"name": "logviewer.client",
"productName": "Compact Log Viewer",
"version": "1.0.6",
"description": "A cross platform Electron app to view and search logs stored in the Compact log format",
"homepage": "https://github.com/warrenbuckley/Compact-Log-Format-Viewer#readme",
"repository": {
"type": "git",
"url": "https://github.com/warrenbuckley/Compact-Log-Format-Viewer.git"
},
"main": "dist/main/main.js",
"scripts": {
"start": "electron .",
"lint": "eslint -c .eslintrc.js --ext .ts src",
"tsc": "tsc --diagnostics",
"watch": "tsc --watch",
"win": "electron-builder --publish never --win --config ./electron-builder.json",
"mac": "electron-builder --publish never --mac --config electron-builder.json"
},
"author": "Warren Buckley",
"license": "ISC",
"private": true,
"devDependencies": {
"@types/angular": "^1.8.0",
"@types/chart.js": "^2.9.29",
"@types/electron-is-dev": "^1.1.1",
"@types/request": "^2.48.5",
"@typescript-eslint/eslint-plugin": "^4.10.0",
"@typescript-eslint/parser": "^4.10.0",
"electron": "^11.1.0",
"electron-builder": "^22.9.1",
"eslint": "^7.16.0",
"eslint-plugin-jsdoc": "^30.7.8",
"eslint-plugin-prefer-arrow": "^1.2.2",
"tslint": "^6.1.3",
"typescript": "^4.1.3",
"typescript-tslint-plugin": "^1.0.1"
},
"dependencies": {
"angular": "^1.8.2",
"angular-chart.js": "^1.1.1",
"bootstrap": "^4.5.3",
"chart.js": "^2.9.4",
"electron-log": "^4.3.1",
"electron-util": "^0.14.2",
"jquery": "^3.5.1",
"popper.js": "^1.16.1",
"request": "^2.88.2"
}
}
enforceMacOSAppLocation
fails in this scenario:
/Applications/Foo.app
Foo.app
, located somewhere else/Applications
enforceMacOSAppLocation
will focus to the currently open Foo v1 windowI suppose the actual moving of the .app
file failed silently but this library was stil trying to open the app and the previously opened one gained the focus this way?
I guess in this situation another dialog should be shown asking the user to close the old app and retrying later.
Applies to
and probably others.
app.getName()
has been deprecated in favor of app.name
(electron) 'getName function' is deprecated and will be removed. Please use 'name property' instead.
require('electron-util')
does not work if the default require
function is replaced by the one provided by the esm loader.
More precisely, this launcher script throws an error when invoked as electron launcher.js
:
require = require('esm')(module)
const { is } = require('electron-util')
while this one works:
const { is } = require('electron-util')
The error I'm getting in the first case is:
TypeError: Cannot read property '__esModule' of undefined
at Object.get (/Users/tamas/.../node_modules/electron-util/source/api.js:5:62)
This is because esm
is doing some magic behind the scenes that tries to access api.__esModule
when ./source/api
is require()
d, which ends up querying electron.remote.__esModule
- but electron.remote
is not available in the main process. The fix would be easy with a new api.js
:
'use strict';
const electron = require('electron');
module.exports = new Proxy(electron, {
get: (target, property) => target[property] || (target.remote ? target.remote[property] : undefined)
})
Using the esm
loader might not be a common setup; in my particular case, I need it because the preload script of my app imports stuff from a shared codebase that happens to use the ES6 module syntax. Of course it would be possible to transpile it to CommonJS, but I believe that the change required in api.js
is so insignificant that it's probably not a stretch to have this fixed in electron-util
instead.
Thanks a lot in advance for your consideration!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.