howler.js Audio Library
howler.js is a robust JavaScript audio library designed for modern web applications, abstracting away the complexities of the Web Audio API and falling back seamlessly to HTML5 Audio when necessary. Currently stable at version 2.2.4, it sees regular maintenance releases addressing browser-specific quirks and improving reliability, such as fixes for Opera versions 100+ and Chrome for iOS. Key differentiators include its single, intuitive API, comprehensive cross-browser compatibility across desktop and mobile, support for audio sprites, 3D spatial audio, and an automatic HTML5 audio node pooling system to overcome common browser autoplay restrictions. It's a dependency-free, lightweight solution, offering full control over playback features like fading, rate, seek, and volume, suitable for a wide range of audio needs from simple sound effects to complex interactive audio experiences. It prioritizes performance with automatic caching and is modular, weighing in at just 7kb gzipped.
Common errors
-
Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
cause Browser autoplay policies prevent audio from playing until a user gesture (click, keydown, etc.).fixCall `sound.play()` only within an event listener for a user interaction. `howler.js` attempts to unlock audio on first input, but direct calls outside user events will fail. -
Howl is not defined
cause The `Howl` class was not correctly imported or loaded into the scope.fixFor ES Modules, use `import { Howl, Howler } from 'howler';`. For CommonJS, use `const { Howl, Howler } = require('howler');`. If using a script tag, ensure `howler.js` is loaded before your script and that `Howl` is accessed globally. -
Sound not looping correctly in Firefox.
cause Browser-specific timing or decoding issues for looping audio files in certain Firefox versions.fixUpdate `howler.js` to version 2.2.2 or newer, which includes specific fixes for looping sounds in recent Firefox desktop versions. -
Audio file with authentication headers fails to load or plays with a network error.
cause The Web Audio API's underlying XHR requests do not automatically send custom headers or `withCredentials` to the server for protected assets.fixWhen creating a new `Howl` instance for such files, set the `xhr` option with the appropriate `headers` or `withCredentials: true` property. This feature is available since `howler.js` v2.2.0.
Warnings
- breaking Browser autoplay policies now universally require user interaction before audio can play. The `mobileAutoEnable` option was renamed to reflect this broader impact.
- gotcha Earlier versions might encounter issues with multiple HTML5 Audio elements failing to play after the first one, due to browser limitations and resource handling.
- gotcha When fetching audio files for the Web Audio API (default), custom HTTP headers (e.g., for authentication) or `withCredentials` settings are not automatically applied to XHR requests.
- gotcha Specific browser versions (e.g., Safari 14, recent Firefox, Chrome for iOS) have had unique quirks affecting audio playback, such as WAV detection, looping behavior, or critical errors.
Install
-
npm install howler -
yarn add howler -
pnpm add howler
Imports
- Howl
const Howl = require('howler');import { Howl } from 'howler'; - Howler
const Howler = require('howler');import { Howler } from 'howler'; - Browser Global
import { Howl } from 'howler';<!-- Add <script src="howler.js"></script> in HTML --> <script> const sound = new Howl({ src: ['sound.mp3'] }); </script>
Quickstart
import { Howl, Howler } from 'howler';
// Initialize a Howl instance for a background music loop
const bgMusic = new Howl({
src: ['/assets/music.mp3', '/assets/music.webm'],
loop: true,
volume: 0.5
});
// Initialize a Howl for a sound effect
const sfx = new Howl({
src: ['/assets/blip.mp3', '/assets/blip.wav'],
volume: 0.8
});
// Global settings: mute all sounds and then unmute after a delay
Howler.mute(true);
console.log('All audio is muted globally.');
setTimeout(() => {
Howler.mute(false);
console.log('All audio is unmuted. Playing background music and sound effect.');
bgMusic.play(); // Play background music
sfx.play(); // Play a sound effect
}, 3000);
// You can also stop all sounds globally if needed
// Howler.stop();