Registry / http-networking / robots-txt-parser

robots-txt-parser

JSON →
library1.0.1jsnpmunverified

The `robots-txt-parser` library provides a lightweight, promise-based solution for parsing `robots.txt` files efficiently in Node.js environments. It is currently at version 2.0.3, offering features such as comprehensive wildcard support in rules, configurable caching of `robots.txt` content, and flexible asynchronous operations via both promises and traditional callbacks. This package is specifically designed for developers building web crawlers, scrapers, and other automated bots that must adhere to website crawling policies. Key differentiators include its focus on Node.js, a clear API for determining URL crawlability, retrieving sitemaps, and managing crawl delays. The project maintains a stable release cadence, with the 2.x major version being actively supported since late 2018. Users can configure critical parameters such as the default user agent string and how the parser evaluates scenarios where allow/disallow rules are balanced.

npm install robots-txt-parser
INSTALL
IMPORT
SIG · ROBOTS-TXT-PARSER
R
robots-txt-parser
http-networkingjavascriptv1.0.1
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.

robotsParser
const robotsParser = require('robots-txt-parser');
The library exports a function as its default, primarily designed for CommonJS environments.
robotsParser
import robotsParser from 'robots-txt-parser';
import { robotsParser } from 'robots-txt-parser';
When using ES Modules, the library's default function export is imported directly. It is not a named export.
RobotsInstance
const robots = robotsParser({ userAgent: 'MyBot' });
The imported `robotsParser` is a function that, when called, returns an instance of the parser with configuration options.

This quickstart demonstrates how to initialize the robots.txt parser, fetch rules for a domain, and check URL crawlability using synchronous, promise-based, and callback methods. It also shows how to retrieve sitemaps.

const robotsParser = require('robots-txt-parser'); const robots = robotsParser({ userAgent: 'Googlebot', allowOnNeutral: false, }); async function checkCrawlability() { try { const domainUrl = 'http://example.com'; await robots.useRobotsFor(domainUrl); // Fetch and parse robots.txt for the domain console.log(`Checking crawlability for ${domainUrl}/news...`); const canCrawlSyncResult = robots.canCrawlSync(`${domainUrl}/news`); console.log(`Crawlable (sync): ${canCrawlSyncResult}`); // Promise-based check const canCrawlPromiseResult = await robots.canCrawl(`${domainUrl}/news`); console.log(`Crawlable (promise): ${canCrawlPromiseResult}`); // Callback-based check robots.canCrawl(`${domainUrl}/articles`, (value) => { console.log(`Crawlable (callback for ${domainUrl}/articles): ${value}`); }); const sitemaps = await robots.getSitemaps(); console.log('Sitemaps found:', sitemaps); } catch (error) { console.error('Error during robots.txt parsing or crawl check:', error); } } checkCrawlability();
Debug
Known issues
breakingMajor version 2.0.0, released in late 2018, included dependency updates (e.g., Axios, Underscore) and internal refactoring. While the core API remained largely consistent, minor breaking changes or behavioral shifts in edge cases might exist for users migrating from 1.x versions. Always review the project's changelog or GitHub releases for specific details if upgrading.
fix
Thoroughly test existing integrations when upgrading from 1.x. Review GitHub release notes for v2.0.0 if available for specific API changes.
affects: >=2.0.0
gotchaThe `canCrawl` method supports both a promise-based API and an optional callback. Mixing these styles or forgetting to await the promise can lead to race conditions, unhandled promise rejections, or inconsistent results, especially in complex asynchronous flows.
fix
Consistently use either the promise-based (`await robots.canCrawl(...)`) or callback-based approach. If using promises, always handle potential rejections with `try...catch` or `.catch()`.
affects: >=1.0.0
gotchaThe `robots.useRobotsFor(url)` method is asynchronous and *must* be awaited or resolved before `canCrawl` methods are called for URLs on that domain. If `useRobotsFor` has not completed, `canCrawl` might return `true` by default (no rules found yet) or throw an error depending on the internal state.
fix
Always ensure `await robots.useRobotsFor(domainUrl)` has successfully completed before making any `canCrawl` calls for URLs under `domainUrl`.
affects: >=1.0.0
gotchaThe behavior of the parser is highly dependent on the configured `userAgent` and `allowOnNeutral` options. Incorrect `userAgent` matching can lead to rules for other bots being applied, and `allowOnNeutral` dictates behavior when allow/disallow rules are equally balanced, which can significantly alter crawl permissions.
fix
Carefully configure `userAgent` to match your bot's identity and understand the implications of `allowOnNeutral` for your crawling strategy. Test with various `robots.txt` files to ensure expected behavior.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: robots.useRobotsFor is not a function
The `robotsParser` function was imported but not called to create an instance, or the instance was not assigned to a variable named `robots`.
fix
Ensure you call the imported function: `const robots = robotsParser({ userAgent: 'MyBot' });`
UnhandledPromiseRejectionWarning: Promise { <pending> }
Asynchronous methods like `robots.useRobotsFor()` or `robots.canCrawl()` return promises, but they were not `await`ed or their `.then()`/`.catch()` handlers were not chained.
fix
Use `await` keyword before promise-returning calls within an `async` function, or chain `.then()` and `.catch()` to handle the promise resolution and rejection.
robots.canCrawl(...) always returns true (or incorrect values)
The `robots.txt` rules for the target domain were either not fetched/parsed successfully, or the `userAgent` configuration did not match any specific rules, causing the default `allowOnNeutral` behavior to take effect.
fix
Verify that `await robots.useRobotsFor(domainUrl)` completes successfully for the domain in question. Check your `userAgent` configuration to ensure it matches specific `User-agent` directives in the `robots.txt` file, or adjust `allowOnNeutral` if the default behavior is undesirable.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
2
Resources
robots-txt-parser — npm install robots-txt-parser · libregistry