DNS Zone File Parser and Generator
The `zone-file` package provides an RFC1035-compliant JavaScript library for both parsing and generating DNS zone files. It supports a comprehensive range of common DNS record types, including Start of Authority (SOA), Name Server (NS), A records (IPv4), AAAA records (IPv6), Canonical Name (CNAME), Mail Exchange (MX), Pointer (PTR), Service (SRV), Text (TXT), and Uniform Resource Identifier (URI) records, alongside the critical `$ORIGIN` keyword for defining the zone's base domain. The current and only stable version available on npm is 1.0.0, which was last published in January 2019. While the library itself is functional for its stated purpose, the lack of new releases under the `zone-file` npm package for several years, despite related code being developed under the `stacks-network/zone-file-js` GitHub repository, indicates that the specific npm package `zone-file` is effectively abandoned. Developers should be aware of this infrequent update cadence and consider potential alternatives for ongoing maintenance and feature support.
Common errors
-
Error: Invalid zone file syntax at line X: Y
cause The input string provided to `parseZone` contains malformed DNS record entries or overall syntax errors according to RFC1035.fixReview the `zoneFileContent` string for correct record types, hostnames (e.g., trailing dots for FQDNs), TTLs, and other DNS specific syntax. Use a DNS zone file validator to pinpoint the exact issue. -
TypeError: zonefile.parseZone is not a function
cause The module was imported incorrectly, or the `zonefile` variable does not properly reference the functions exported by the package.fixEnsure you are using the correct import statement: `import { parseZone, generate } from 'zone-file';` for ESM, or `const { parseZone, generate } = require('zone-file');` for CommonJS.
Warnings
- breaking The `zone-file` npm package (v1.0.0) has not been updated since January 2019. While the underlying code may be developed under a different GitHub repository (`stacks-network/zone-file-js`), no new versions have been published to npm under this package name. This means it lacks recent bug fixes, performance improvements, and security updates.
- gotcha Incorrectly formatted zone file input can lead to parsing errors or the generation of invalid DNS configurations, which can cause significant service disruptions. DNS zone files are highly sensitive to syntax, including trailing dots on FQDNs and correct record structures.
- gotcha The `zone-file` package uses CommonJS `require()` internally and exports functions for `import` in modern JavaScript environments. If using `require()` directly, ensure your environment is set up for CommonJS or that you destructure correctly.
Install
-
npm install zone-file -
yarn add zone-file -
pnpm add zone-file
Imports
- parseZone
const { parseZone } = require('zone-file');import { parseZone } from 'zone-file'; - generate
const { generate } = require('zone-file');import { generate } from 'zone-file';
Quickstart
import { parseZone, generate } from 'zone-file';
const zoneFileContent = `
$ORIGIN example.com.
$TTL 3600
@ IN SOA ns1.example.com. hostmaster.example.com. (
2023010101 ; Serial
7200 ; Refresh
3600 ; Retry
1209600 ; Expire
3600 ; Minimum TTL
)
@ IN NS ns1.example.com.
@ IN NS ns2.example.com.
@ IN A 192.0.2.1
www IN A 192.0.2.2
mail IN MX 10 mail.example.com.
mail IN A 192.0.2.3
`;
try {
// Parse the zone file content into a JavaScript object
const parsedZone = parseZone(zoneFileContent);
console.log('Parsed Zone Object:');
console.log(JSON.stringify(parsedZone, null, 2));
// Add a new record to the parsed object
parsedZone.cname = parsedZone.cname || [];
parsedZone.cname.push({ name: 'blog', alias: 'www' });
// Generate a new zone file string from the modified object
const generatedZone = generate(parsedZone);
console.log('\nGenerated Zone File with new CNAME record:');
console.log(generatedZone);
} catch (error) {
console.error('Error processing zone file:', error.message);
}