A small Node.js SEO crawler achieves an automated audit of a website's on-page elements to ensure they meet search engine optimization standards. By programmatically visiting every URL on a site, the crawler identifies missing metadata, broken links, and structural errors that would otherwise require manual inspection. To build this, you need a basic understanding of JavaScript and a Node.js environment installed on your machine.

What You'll Need

  • Node.js (LTS version recommended)
  • A terminal or command prompt
  • An HTTP client library (such as axios or node-fetch) to request page content
  • An HTML parser (such as cheerio or jsdom) to traverse the DOM and extract data
  • A way to track visited URLs (such as a Set or a simple database) to prevent infinite loops

Step-by-Step

  1. Fetch the Page Content: Use your HTTP client to send a GET request to the target URL. Ensure you set a User-Agent header to mimic a real browser; otherwise, some servers may block the request as a bot. Capture the response body as a string of HTML.
  2. Parse the HTML: Load the HTML string into your parser. This allows you to use CSS selectors (like .title or #main) to find specific elements without using complex regular expressions, which are prone to errors when parsing HTML.
  3. Validate the Title Tag: Check for the existence of the <title> tag. Once found, verify that it is not empty. Check the character length; generally, titles should be between 50 and 60 characters to avoid truncation in search results. Flag any page that is missing a title or has one that is excessively long or short.
  4. Inspect the Meta Description: Look for the <meta name="description"> tag. Verify that the content attribute is present and populated. Check the length of this description, aiming for 150 to 160 characters. A missing meta description often results in search engines generating a random snippet from the page content, which may not be optimized for clicks.
  5. Analyze Heading Hierarchy: Locate all heading tags from <h1> to <h6>. Specifically, ensure there is exactly one <h1> per page, as this signals the primary topic to search engines. Check that headings follow a logical order (e.g., an <h3> should not appear before an <h2>).
  6. Check for Image Alt Text: Find all <img> tags and verify that the alt attribute exists and is not empty. Images without alt text are invisible to screen readers and search engine bots, reducing the accessibility and SEO value of the page.
  7. Verify Canonical Tags: Search for <link rel="canonical" href="...">. This tag tells search engines which version of a URL is the master copy. Ensure the canonical URL matches the current page URL or points to the intended primary version to prevent duplicate content issues.
  8. Scan for Broken Internal Links: Extract all <a href="..."> tags. Filter for internal links (those pointing to the same domain). For each link, perform a HEAD request to check the HTTP status code. Any response in the 400s (like 404 Not Found) or 500s should be flagged as a broken link.
  9. Check Page Load Speed (Basic): While a full performance audit requires a headless browser, a small crawler can measure the time between sending the request and receiving the full HTML response. If this time exceeds a certain threshold (e.g., 2 seconds), mark the page for a speed optimization review.
  10. Validate Robots.txt and Meta Robots: Check for <meta name="robots"> tags. If the tag is set to noindex, the crawler should note that the page is intentionally hidden from search engines. Cross-reference this with the site's robots.txt file to ensure there are no conflicting instructions.

Common Mistakes

One of the most frequent errors is failing to handle redirects. If a page returns a 301 or 302 status code, a naive crawler might treat the redirect response as the final page content, leading to incorrect metadata analysis. Always follow the redirect chain to the final destination before parsing the HTML.

Another common mistake is ignoring the robots.txt file. Crawling a site without checking its rules can lead to your IP being blocked or the crawler accessing sensitive directories that the site owner intended to keep private. Always fetch and parse /robots.txt first to determine which paths are disallowed.

Developers often forget to implement a delay between requests. Sending hundreds of requests per second can overwhelm a small server, causing it to crash or trigger a Rate Limit (HTTP 429). Implementing a small sleep timer or using a concurrency limit is essential for a polite crawler.

Using regular expressions to parse HTML is a significant pitfall. HTML is not a regular language, and small variations in attribute ordering or whitespace can cause regex patterns to fail. Always use a dedicated DOM parser like Cheerio to ensure reliability.

Tips

To make your crawler more efficient, implement a "seen" list using a Set. Before visiting a URL, check if it exists in the Set; if it does, skip it. This prevents the crawler from getting stuck in infinite loops caused by circular linking structures.

Use a logging library instead of console.log. When crawling thousands of pages, you need a way to categorize errors (e.g., WARN for missing alt text, ERROR for 404s) and save them to a file for later analysis rather than scrolling through a terminal window.

If you need to check content that is rendered via JavaScript (like React or Vue apps), a simple HTTP request will not work because it only retrieves the initial empty HTML shell. In these cases, integrate a headless browser like Puppeteer or Playwright to render the page before parsing.

Implement a depth limit. If you are crawling a massive site, you may not want to go ten levels deep into the architecture. Set a maximum depth variable to keep the crawl focused on the most important pages.

Troubleshooting

If the crawler is receiving 403 Forbidden errors, the server is likely detecting the bot. The first step is to check your User-Agent header. Change it to a common browser string, such as a Chrome or Firefox agent. If that fails, check if the site requires specific cookies or headers to grant access.

If the crawler is skipping pages or missing links, verify your URL normalization logic. A link to /about and /about/ are technically different URLs but usually lead to the same page. Ensure you strip trailing slashes and convert all URLs to a consistent format before adding them to the "seen" list.

If the parser is returning null for elements that you can see in your browser, check the page source (Ctrl+U). If the element is missing from the source but visible in the browser, the content is being injected by JavaScript. You will need to switch from a static parser to a headless browser.

If the crawler crashes due to memory leaks, check how you are storing the visited URLs. For very large sites, a JavaScript Set in memory may exceed the heap limit. In such cases, move the visited URL list to a lightweight database like SQLite or Redis.

Frequently Asked Questions

Does a small crawler need to handle CAPTCHAs?

Generally, no. If a site presents a CAPTCHA, it is actively trying to block automated access. A small SEO crawler should respect these barriers and flag the page as "unreachable" rather than attempting to bypass security measures.

How often should I run an SEO crawler?

For most small to medium sites, a weekly or monthly crawl is sufficient to catch new errors. For sites with daily content updates, a daily crawl of only the new or modified pages is recommended.

Can this crawler check for duplicate content?

Yes, by hashing the main content area of each page (e.g., the <main> or <article> tag) and comparing those hashes. If two different URLs produce the same content hash, they are likely duplicates.

Is it legal to crawl a website?

Crawling public data is generally legal, but you must adhere to the site's Terms of Service and the robots.txt file. Avoid aggressive crawling that disrupts the site's availability for human users.

Should I check for 404s on every single crawl?

Yes, because internal links can break at any time due to page deletions or URL changes. Checking for broken links is one of the most valuable functions of an automated crawler.

What is the best way to export the results?

Exporting to a CSV file is usually best for small crawlers. This allows you to open the results in a spreadsheet, filter by error type, and share the list with a developer or content editor for fixing.