Registry / http-networking / multicast-dns

multicast-dns

JSON →
library7.2.5jsnpmunverified

multicast-dns is a low-level, pure JavaScript implementation of the Multicast DNS (mDNS) protocol, widely used for zero-configuration service discovery, commonly known as Apple's Bonjour or Avahi. It facilitates device and service discovery on a local network by sending DNS queries over UDP multicast to the standard address `224.0.0.251:5353` and listening for corresponding responses. The current stable version, 7.2.5, provides an event-driven API for both querying for and responding to mDNS packets. It supports various DNS record types including A, AAAA, PTR, SRV, TXT, and HINFO, allowing applications to discover network services and resolve local hostnames. The library offers fine-grained control over network interfaces, ports, and other UDP socket options, making it a foundational component for peer-to-peer and local area network applications that require automatic service registration and discovery without relying on a central DNS server. It maintains a focus on low-level network interactions rather than abstracting into a high-level service browser.

npm install multicast-dns
INSTALL
IMPORT
SIG · MULTICAST-DNS
M
multicast-dns
http-networkingjavascriptv7.2.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mdns instance (CommonJS)
const mdns = require('multicast-dns')();
const mdns = require('multicast-dns');
The `multicast-dns` package exports a factory function that must be immediately invoked to create an mDNS instance.
mdns instance (ESM)
import createMdns from 'multicast-dns'; const mdns = createMdns();
import { mdns } from 'multicast-dns';
For ESM, import the default export (the factory function) and then call it to get a new mDNS instance. The package's primary documentation showcases CommonJS usage.
Packet Types (TypeScript)
import type { Packet, Question, Answer } from 'multicast-dns';
import { Packet } from 'multicast-dns';
While the library is JavaScript, TypeScript type definitions are often available via `@types/multicast-dns` for enhanced development experience. Common types include `Packet`, `Question`, and `Answer` for handling mDNS data structures.

This quickstart initializes a `multicast-dns` instance, logs incoming query and response packets, demonstrates how to query for specific records, and how to respond to queries for a known service, then destroys the instance.

import createMdns from 'multicast-dns'; const mdns = createMdns(); mdns.on('response', function(response) { console.log('Got a mDNS response packet:', JSON.stringify(response, null, 2)); }); mdns.on('query', function(query) { console.log('Got a mDNS query packet:', JSON.stringify(query, null, 2)); // Example: Respond to a query for 'my-service.local' if (query.questions[0] && query.questions[0].name === 'my-service.local') { mdns.respond({ answers: [{ name: 'my-service.local', type: 'A', ttl: 120, data: '127.0.0.1' // Replace with actual IP }] }); console.log('Responded to query for my-service.local'); } }); // Query for an A record for a local hostname (e.g., your-hostname.local) mdns.query({ questions:[ { name: 'your-hostname.local', type: 'A' }, { name: 'my-service.local', type: 'SRV' } ] }); console.log('Sent mDNS query for your-hostname.local and my-service.local'); // Destroy the instance after a timeout to prevent resource leaks in short-lived scripts setTimeout(() => { mdns.destroy(); console.log('mDNS instance destroyed.'); }, 10000);
Debug
Known issues
gotchaThe `reuseAddr` option for the underlying UDP socket requires Node.js version 0.11.13 or newer. Using it on older versions will result in an error or unexpected behavior.
fix
Ensure your Node.js runtime is updated to a modern version (LTS recommended) or avoid using the `reuseAddr: true` option.
affects: <0.11.13
gotchaThe mDNS protocol operates over UDP port 5353, which is a well-known port. Firewalls or other services (like Bonjour/Avahi daemons) might block access or already be using this port, leading to `EADDRINUSE` or `EACCES` errors when binding the socket.
fix
Ensure port 5353 is open in your firewall for UDP traffic, or configure a different `port` option (though this would break standard mDNS discovery). If another service is listening, stop it or configure `multicast-dns` to use a different `interface` if available.
affects: >=1.0.0
gotchaThe README and examples primarily demonstrate CommonJS (`require()`) usage. While modern Node.js supports ESM, careful consideration is needed for `import` statements, especially when the package exports a factory function that needs to be invoked immediately.
fix
For ESM, use `import createMdns from 'multicast-dns'; const mdns = createMdns();`. Avoid named imports like `{ mdns }` unless explicitly documented, as the default export is a function.
affects: >=6.0.0 (Node.js versions with ESM support)
Errors
Common errors & fixes
Error: bind EADDRINUSE 224.0.0.251:5353
Another process (e.g., a system mDNS responder like Bonjour or Avahi, or another instance of `multicast-dns`) is already listening on the default mDNS port and multicast address.
fix
Stop the conflicting process, or configure your `multicast-dns` instance to bind to a specific `interface` if available, or a different `port` (which will prevent standard mDNS discovery for that instance).
Error: bind EADDRNOTAVAIL
The specified network `interface` in the options (e.g., `interface: '192.168.0.2'`) does not exist or is not available on the current system.
fix
Verify the IP address configured for the `interface` option is correct and corresponds to an active network interface on your machine. Remove the `interface` option to let `multicast-dns` bind to all available interfaces (default behavior).
TypeError: multicastdns is not a function
The imported/required `multicast-dns` module was not invoked as a function to create an instance. For example, `const mdns = require('multicast-dns');` instead of `const mdns = require('multicast-dns')();`.
fix
Ensure you are calling the imported module as a function: `const mdns = require('multicast-dns')();` for CommonJS, or `const mdns = createMdns();` after `import createMdns from 'multicast-dns';` for ESM.
Upgrade
Version history
7.2.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
multicast-dns — npm install multicast-dns · libregistry