28

How to replace the function of javascript prompt in electron?

Can someone give me an example?

I tried to use the function prompt, but got an error:

Uncaught Error: prompt() is and will not be supported.

Akshay Anurag
  • 724
  • 1
  • 8
  • 27
Thales
  • 545
  • 1
  • 6
  • 17
  • I'm guessing a framework that lets you create cross-platform apps, based on Node.js, doesn't neccessarely support browser methods, like alert, prompt etc. for quite obvious reasons, there's no browser to display it in. – adeneo Aug 09 '16 at 19:28
  • 5
    @adeneo Electron is based on Chromium in every platform, so yes there's a browser and it is always the same :) – Alex Aug 10 '16 at 18:00

2 Answers2

35

prompt, confirm and alert are functions which blocks the execution thread of the script until a user input and that's the reason electron team didn't supported it. Instead you can use some third party package for the same reason.

Here are some packages which provides this functionality in async way

https://www.npmjs.com/package/smalltalk

https://www.npmjs.com/package/vex-js

https://www.npmjs.com/package/dialogs

Zayn Ali
  • 4,765
  • 1
  • 30
  • 40
5

My answer is a little late but maybe still helpful for others.

Since the Electron team does not want to implement the prompt() behavior themselves, I developed this solution: electron-osx-prompt. It provides a Promise-based way to get some simple user input and adapts to the macOS styling.

// From renderer or main process, doesn't matter
const userPrompt = require('electron-osx-prompt');

const icon = __dirname + '/icon.png';

userPrompt('Label text', 'Placeholder text', icon)
  .then(input => {
    console.log(input);
  })
  .catch(err => {
    console.log(err);
  });
Peter Freeman
  • 81
  • 1
  • 3
  • 1
    I used this and found for Electron 5.x+, you need to edit the index.js file and add webPreferences { nodeIntegration: true } in the BrowserWindow creation process. – Derek Wade Mar 18 '21 at 21:57