I am using electron app.setLoginItemSettings(settings) to enable my application to autostart at login. This is how I am implementing it:
const executable_path = process.execPath;
const exeName = path.basename(process.execPath);
...
const createWindow = () => {
...
ipcMain.on('close-me', (evt, arg) => {
var boot = settings.get("startatboot");
if (boot) {
app.setLoginItemSettings({
openAtLogin: true,
path: executable_path,
name: exeName,
args: [
'--processStart', `"${exeName}"`,
'--process-start-args'
]
})
} else {
app.setLoginItemSettings({
openAtLogin: false,
openAsHidden: false
});
}
app.quit()
});
...
};
And this result in the following inside the register (Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run)
Test Autorun.exe for name
C:\Program Files\Test Autorun\Test Autorun.exe --processStart "Test Autorun.exe" --process-start-args for data
Now inside my package.json file I defined the following:
{
"name": "apptest",
"productName": "Test Autorun",
"version": "1.0.0",
"description": "Test Autorun",
"main": "src/index.js",
"scripts": {
"start": "electron-forge start",
"package": "electron-forge package",
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint": "echo \"No linting configured\"",
"build-installer": "electron-builder"
},
"build": {
"appId": "apptest",
...
Now my problem is that the application is not running on start and I have no idea why this is happening, I tried changing a lot of the values to end up with this register content. All the attempts before were showing as different programs inside windows startup settings.
I have got to fill what I think is correct data inside the registers but there is something I am missing but I have no idea what it is. Any help would go a long way, thanks.
EDIT:
When I launch the app normally it always asks the user to allow the app with unknown publisher to make changes to the device. I don't know if that is why the app is not running on start or if the app would run showing the same message awaiting user approval.
EDIT2:
it is adding the register inside the HKEY_CURRENT_USER and not the HKEY_LOCAL_MACHINE
As I suspected, it was related to the unknown publisher popup; so, when I edited the registry entry related to UAC by using the following command line code shown below, the application started on boot.
%windir%\System32\reg.exe ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
Source.
Related
When I install the electron quick start and run
node_modules/.bin/electron --help
it tells me:
A path to an Electron app may be specified. It must be one of the following:
- index.js file.
- Folder containing a package.json file.
- Folder containing an index.js file.
- .html/.htm file.
- http://, https://, or file:// URL.
Running
node_modules/.bin/electron .
(i.e. the option "Folder containing a package.json file") works alright. package.json points to main.js and looks as follows:
{
"name": "electron-quick-start",
"version": "1.0.0",
"description": "A minimal Electron application",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"repository": "https://github.com/electron/electron-quick-start",
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "^4.1.4"
}
}
I'd assume that running this:
node_modules/.bin/electron main.js
should work as well, but it does not – it just shows the Electron "dummy" screen.
How can I specify on the command line which JavaScript to run?
Turns out the JavaScript is executed alright – the problem is how the path to the main HTML file is resolved. Solution: Replace:
mainWindow.loadFile('index.html')
with:
const arg = process.argv[1] // better command line parsing might be needed
const dir = fs.lstatSync(arg).isDirectory() ? arg : path.resolve(arg, '..')
mainWindow.loadFile(path.resolve(dir, 'index.html'))
Electron-builder Version: 20.38.5
Target: Windows ia32 and x64
I'm trying to push the project to GitHub releases in a private repository.
The main target is Win ia32 and x64.
I set the token in the console by:
set GH_TOKEN=<blabla>
And at the package.json file:
"publish": {
"provider": "github",
"repo": "electron-todo-tasks",
"owner": "AndreD23",
"host": "github.com",
"protocol": "https",
"releaseType": "draft",
"token": "<blablabla>",
"private": true
}
I'm the only one who will have access to the project, so updating the token in the file apparently not a problem.
But, when I run:
electron-builder --x64 --ia32 --publish always
It shows me the error:
Error: Cannot cleanup:
Error #1 --------------------------------------------------------------------------------
HttpError: 404 Not Found
"method: undefined url: https://github.com/repos/AndreD23/electron-todo-tasks/releases\n\nPlease double check that your authentication token is correct. Due to security reasons actual status maybe not reported, but 404.\n"
I'll attach the whole error.
2019-03-22T13_04_27_992Z-debug.log
electron publish error.txt
I tried to change the option always to onTag or onTagOrDraft and change releaseType to release.
I tried to manually create a draft on GitHub first, doesn't work.
I tried to make the repository public for tests, not working too.
I tried to regenerate the token twice and updated the envs, console and package.json, doesn't work.
Is there any configuration that I've missed? What I have to do for Electron to publish to GitHub?
At the error, it says that cannot find https://github.com/repos/AndreD23/electron-todo-tasks/releases. Opening it on the web browser, it returns 404. I find the link a bit strange because it adds the word repos after github.com, and removing it in the browser it loads fine. This word is setting automatically?
Thanks #edelCustodio
Reference: https://github.com/electron-userland/electron-builder/issues/3776#issuecomment-486888574
This worked for me, on PowerShell type this
[Environment]::SetEnvironmentVariable("GH_TOKEN","my-personal-token","User")
and be sure that you package.json will be like this:
"build": {
"appId": "com.friasoftit.electron-auto-update",
"productName": "Electron Auto Update",
"publish": [
{
"provider": "github",
"owner": "edelCustodio",
"repo": "electron-auto-update"
}
],
"win": {
"publish": [
"github"
],
"target": [
{
"target": "nsis",
"arch": [
"x64",
"ia32"
]
}
]
}
},
And then I run this command yarn run publish and this is how my publish is on my package.json
"publish": "build --win -p always"
Don't forget to close and open a new PowerShell terminal.
Run windows powerShell from Administrator
Execute [Environment]::SetEnvironmentVariable("GH_TOKEN","<git_hub_token>","User")
Launch or full restart your IDE
Run bozon package windows --publish
Config:
{
"name": "",
"version": "",
"description": "",
"author": "",
"repository": "<repo_url>",
"dependencies": {
"cross-fetch": "^3.0.6",
"electron-builder": "^22.10.5",
"file-loader": "^6.2.0",
"luxon": "^1.26.0",
"node-fetch": "^2.6.1"
},
"license": "ISC",
"devDependencies": {
"bozon": "1.1.3",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"jest": "26.6.3",
"spectron": "13.0.0"
},
"build": {
"appId": "",
"publish": [
{
"provider": "github"
}
],
"win": {}
},
"main": ".eslintrc.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
While in developing mode, executing the exe is working well.
My code to launch the exe is
function LaunchExe() {
var child = require('child_process').execFile;
var executablePath = 'DemoExe/Sample.exe';
var parameters = ['Hai', 'Test', 'Dat'];
child(executablePath, parameters, function (err, data) {
console.log(err)
console.log(data.toString());
});
}
But after packaging the Electron app, I can't launch the exe.
The command I use to build the exe is
electron-packager . --asar
Error code
Error: spawn DemoExe/Sample.exe ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:232)
at onErrorNT (internal/child_process.js:407)
at process._tickCallback (internal/process/next_tick.js:63)
Regards.
Is your project with angular?
Maybe this can help, but I had problems too with electron-packager.
So I suggest you to use electron builder : https://www.electron.build
Fist you need to add a file at root called electron-builder.json and it shoul'd contain the following (you need to update productname and icons location):
{
"productName": "projectname",
"appId": "org.project.projectname",
"artifactName": "${productName}-setup-${version}.${ext}",
"directories": {
"output": "builds/"
},
"files": [
"dist/",
"node_modules/",
"package.json",
"**/*",
"!**/*.ts",
"!*.code-workspace",
"!package-lock.json",
"!src/",
"!e2e/",
"!hooks/",
"!angular.json",
"!_config.yml",
"!karma.conf.js",
"!tsconfig.json",
"!tslint.json"
],
"nsis": {
"oneClick": false,
"allowToChangeInstallationDirectory": true
},
"mac": {
"icon": "src/favicon.ico"
},
"win": {
"icon": "src/favicon.ico"
},
"linux": {
"icon": "src/favicon.png"
}
}
Then, you need to add those scripts to your package.json
"scripts": {
"build:prod": "npm run build -- -c production",
"package:windows": "npm run build:prod && electron-builder build --windows"
}
can compile the executable with
npm run package:windows
Also, don't forget to add electron builder into your dev dependencies :
yarn add electron-builder --dev
the executable with be located into /builds
Got to work with following steps,
1.package the Electron app using command
electron-packager .
2.Path to exe
path.join(__dirname, "DemoExe", "Sample.exe")
I am using browserify to merge all the .js files of my app into a dist/main.js. My package.json looks like:
"main": "./dist/main.js",
"scripts": {
"start": "electron ./dist/main.js",
},
"bin": {
"electron": "./node_modules/.bin/electron"
}
and I can correctly run my application with npm run start.
However if in main.js I use app.getAppPath() I get:
/home/myuser/projects/electronProject/node_modules/electron/dist/resources/default_app.asar
I would expect this to be
/home/myuser/projects/electronProject/dist/main.js
Did I misunderstood the usage of this method? How can I get the path of the Electron program entrypoint? What is the role of default_app.asar?
Thanks
Why aren't you using __dirname (node.js) or process.resourcesPath (electron)?
https://github.com/electron/electron/blob/master/docs/api/process.md
https://nodejs.org/docs/latest/api/globals.html#globals_dirname
It returns the current application directory:
app.getAppPath()
Returns String - The current application directory.
From the docs.
An asar file is a simple archive format that just appends the files to each other. I'm not sure exactly how you're building the application but tools like electron-packager and electron-builder output the files into a resources/app.asar archive and run the files from there. That means that your current application directory is going to be something/resources/app.asar. From there your main file is located at something/resources/app.asar/main.js.
For whom may ran into the same problem...
It's maybe a problem with your electron configuration field main in package.json
The script specified by the main field is the startup script of your
app, which will run the main process.
The example code from offical websites:
{
"name": "your-app",
"version": "0.1.0",
"main": "main.js",
"scripts": {
"start": "electron ."
}
}
app.getAppPath() output:
YOUR_PATH_TO/electron-quick-start
If you change the code snippet to
{
"name": "your-app",
"version": "0.1.0",
"main": "main.js",
"scripts": {
"start": "electron YOUR_PATH_TO/main.js"
}
}
Then app.getAppPath() output:
YOUR_PATH_TO/electron-quick-start/node_modules/electron/dist/resources/default_app.asar
So the consolution is : If you want to change the startup script, change it in the main field, not just change it in scritps field...
I'm trying to package my recent electron app. I tried to use electron-packager and I actually build the app though it is a bunch of files.
Now I'm facing some problems:
How do I change the app icon? I did it by setting its path in BrowserWindow creation, but shouldn't I be able to do it with electron packager?
And what about the actual desktop icon?
And finally how should I do to package all the files built by electron-packager in one .exe?
I'm building it in windows for windows architecture.
Oh i have faced that problem. first of all i'll leave you an example of how to use the API of electron-packager
var packager = require('electron-packager');
var options = {
'arch': 'ia32',
'platform': 'win32',
'dir': './',
'app-copyright': 'Paulo Galdo',
'app-version': '2.0.5',
'asar': true,
'icon': './app.ico',
'name': 'TierraDesktop',
'ignore': ['./releases', './.git'],
'out': './releases',
'overwrite': true,
'prune': true,
'version': '1.3.2',
'version-string':{
'CompanyName': 'Paulo Galdo',
'FileDescription': 'Tierra de colores', /*This is what display windows on task manager, shortcut and process*/
'OriginalFilename': 'TierraDesktop',
'ProductName': 'Tierra de colores',
'InternalName': 'TierraDesktop'
}
};
packager(options, function done_callback(err, appPaths) {
console.log(err);
console.log(appPaths);
});
Here i left you the link with all the information. Electron packager API
And finally a module so you can build your installer easily, just follow their API.
Windows installer
electron-builder is an all in one packager that will create a Windows installer (or other platforms) and allow you to set icons and many other settings. It actually uses electron-packager as a dependency for the actual packaging.
https://github.com/electron-userland/electron-builder
In Package.json
{
"name": "APP_NAME",
"version": "6.1.0",
"description": "About APP",
"main": "main.js",
"scripts": {
"preinstall": "npm i -g electron-packager",
"start": "electron .",
"package-win32": "electron-packager ./ APP_NAME --overwrite --platform=win32 --arch=ia32 --icon=assets/YOUR_Icon.ico --out=release-builds --app-copyright=\"Copyright © COMPANY_NAME 2020\" --version-string.CompanyName=COMPANY_NAME"
},
"author": "YOUR NAME",
"license": "YOUR LICENSE",
"devDependencies": {
"electron": "^2.0.0",
"electron-packager": "^12.2.0"
}
}