Using Howler.js in Node.js Without a Browser

While howler.js is a highly popular audio library for web applications, it cannot be run natively in a standard Node.js environment without a browser. This article explains why howler.js relies on browser-specific APIs, how you can work around this limitation using headless environments, and the best native Node.js alternatives for playing audio on the server side.

Why Howler.js Does Not Work Natively in Node.js

Howler.js is designed specifically for the client side. Under the hood, it relies on two primary browser technologies:

Node.js is a JavaScript runtime built on Chrome’s V8 engine, but it does not include the Web APIs found in web browsers. If you attempt to import and run howler.js directly in a Node.js script, the runtime will throw errors such as window is not defined or document is not defined because these global browser objects do not exist in Node.js.

Workarounds for Using Howler.js in Node

If you must use howler.js within a Node.js project (for example, to run automated tests or share code), you have two main workarounds:

1. Mocking the Browser with JSDOM

You can simulate a browser environment inside Node.js using a library like jsdom. This allows howler.js to load without throwing “undefined” errors, though it may still lack actual audio hardware output.

const { JSDOM } = require("jsdom");
const { window } = new JSDOM(`<!DOCTYPE html><html><body></body></html>`);

global.window = window;
global.document = window.document;
global.navigator = window.navigator;

// Now you can require howler
const { Howl, Howler } = require('howler');

2. Headless Browsers

If you need to actualize the audio or test howler.js in a real environment using Node.js, you can run a headless browser using Puppeteer or Playwright. This launches a hidden Chromium instance controlled by your Node.js code, allowing howler.js to run with full Web Audio API support.

Native Node.js Audio Alternatives

If your goal is simply to play, stream, or manipulate audio in a Node.js backend or command-line application, you should avoid howler.js and use libraries designed specifically for Node.js: