{

How to trigger System Bell from NodeJS code


How to measure time taken by function to execute Javascript example

A system Beep bell is a sound that plays when a new device is connected or disconnected in Operating System(Windows and macOS)

Sometimes, If you are running batch scripts, you need to initiate System bell after completing node scripts.

This tutorial shows multiple ways to trigger the System beep bell sound in Windows from the Nodejs program.

Making sound is a part of an underline operating system’s native calls. So you need to make native calls to trigger the bell sound. This also makes a sound when an error occurred.

How to initiate System bell sound from Nodejs Code

The system bell is dependent on underline hardware and sound settings.

There are multiple ways we can do it.

  • print the BEL character

BEL character is represented in escape code character (\u0007 or \x07)

console.log('\x07')
console.log('\007')
console.log('\u0007')

or you can also use process.stdout.write to log BEL character

process.stdout.write('\x07');
process.stdout.write('\007');
process.stdout.write('\u0007');

It works in the native terminal. However, It does not work in terminals used in Editors such as Visual studio code and Intelli IDE.

  • Call windows MessageBeep API

Sometimes, the above BEL character will not work on terminals.

This method is to call PowerShell executable beep sound in Windows and play the Glass.aiff file in MACOS.

It uses and imports the child_process node module

Here is a code example


const childProcess = require("child_process");

// Winndows code
childProcess.exec("powershell.exe [console]::beep(500,1200)");
// MACOS code
childProcess.exec("afplay /System/Library/Sounds/Glass.aiff");

Conclusion

To summarize, Learned multiple ways to trigger the System Bell sound from the nodejs application.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.