Notification - Electron Error - _crypto.default.randomFillSync is not a function

1.1k Views Asked by At

I try to use notify in my electron project on node server. So i installed node-notifier module in my app folder using the following command.

  $ npm install --save node-notifier

After that i add button to the my web page to show notify message.
When the button is clicked by user then below js method is ran:

<script type = "text/javascript">
         const notifier = require('node-notifier')
         const path = require('path');
         
         document.getElementById('notify').onclick = (event) => {
            notifier.notify ({
               title: 'My awesome title',
               message: 'Hello from electron, Mr. User!',
               icon: path.join('','images/images.png'),  // Absolute path (doesn't work on balloons)
               sound: true,  // Only Notification Center or Windows Toasters
               wait: true    // Wait with callback, until user action is taken against notification
            }, function (err, response) {
               // Response is response from notification
            });

            notifier.on('click', function (notifierObject, options) {
               console.log("You clicked on the notification")
            });

            notifier.on('timeout', function (notifierObject, options) {
               console.log("Notification timed out!")
            });
         }
      </script>

But when i click my notify button i get the error like as below:

Uncaught TypeError: _crypto.default.randomFillSync is not a function
        rng @ Project\MyElectronProject\node_modules\uuid\dist\rng.js:19
        v4  @ Project\MyElectronProject\node_modules\uuid\dist\v4.js:17
        getPipeName @ Project\MyElectronProject\node_modules\node-notifier\notifiers\toaster.js:51
        notifyRaw@ Project\MyElectronProject\node_modules\nodenotifier\notifiers\toaster.js:60
        document.getElementById.onclick @notification_index.html:16 

Above error is thrown rng function in rng.js.

function rng() {
  if (poolPtr > rnds8Pool.length - 16) {
    debugger;
    _crypto.default.randomFillSync(rnds8Pool);

    poolPtr = 0;
  }

randomFillSync method is not exist. I can't understand this error, have you encountered this error before ? Thanks for your suggestions.

node version that i used in my project: v16.13.1

1

There are 1 best solutions below

0
kevin wang On

node-notifier is a nodejs module, can't be used in browser script.

i think you should call node-notifier in main process, and then communicate from a renderer process to the main process.

Please refer: https://www.electronjs.org/docs/latest/api/ipc-renderer