Registry / testing / next-validate-link

next-validate-link

JSON →
library1.6.4jsnpmunverified

next-validate-link is a utility designed to check and ensure the validity of links within Markdown files. It supports various modern web frameworks, including Next.js, Astro, Nuxt, Waku, and React Router, by offering specialized presets for their routing conventions. The package is currently stable at version 1.6.4, with a consistent release cadence of minor and patch updates, indicating active development and continuous improvement. Its core functionality involves scanning specified directories for Markdown files and then validating both internal and external URLs, as well as relative file paths, providing comprehensive link integrity checks essential for documentation sites and content-heavy applications. A key differentiator is its extensibility with custom remark plugins and customizable Markdown validators, ensuring flexibility for diverse project needs.

npm install next-validate-link
INSTALL
IMPORT
SIG · NEXT-VALIDATE-LINK
N
next-validate-link
testingjavascriptv1.6.4
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.

validateFiles
import { validateFiles } from 'next-validate-link';
const validateFiles = require('next-validate-link');
The library primarily uses ESM, so named imports are standard. CommonJS `require` might not work directly or requires specific transpilation.
scanURLs
import { scanURLs } from 'next-validate-link';
const scanURLs = require('next-validate-link');
Used to scan and predict available URLs based on file-system routing, often for framework-specific presets like Next.js or Astro.
printErrors
import { printErrors } from 'next-validate-link';
import printErrors from 'next-validate-link';
This helper function is typically used after `validateFiles` to format and log validation results to the console, often with an option to exit with a non-zero code on errors.

This quickstart demonstrates how to set up `next-validate-link` to scan your project's Markdown files, resolve internal and external links using framework presets, and report any validation errors. It includes common configuration options for different link types and integrates the `printErrors` helper for clear output.

import { validateFiles, scanURLs, printErrors, readFiles } from 'next-validate-link'; import path from 'node:path'; import process from 'node:process'; async function runValidation() { const projectRoot = process.cwd(); // Adjust to your project root if necessary // Example: target all markdown and mdx files in a 'docs' directory const markdownGlob = path.join(projectRoot, 'docs/**/*.{md,mdx}'); // Scan available URLs based on your framework (e.g., 'next', 'astro', 'nuxt', 'waku', 'react-router') // For Next.js, it will auto-detect pages from your file system. const scannedUrls = await scanURLs({ preset: 'next', // Use 'next' for Next.js projects // pages: ['/custom-page-path', '/another-page'], // Override auto-detected pages if needed }); // Read the markdown files to be validated const filesToValidate = await readFiles(markdownGlob); const validationOptions = { scanned: scannedUrls, // Provide the URLs scanned from your project checkRelativePaths: true, // Validate relative file paths (e.g., `../foo.md`) checkRelativeUrls: true, // Validate relative URLs (e.g., `/some-page`) checkExternal: true, // Validate external HTTP/HTTPS links (requires network access) baseUrl: 'https://your-live-site.com', // Base URL for resolving absolute paths // pathToUrl: (file) => `/docs/${path.basename(file, path.extname(file))}`, // Custom path-to-URL resolver // You can also pass custom remark plugins or a custom Markdown validator function }; console.log(` Validating markdown links in: ${markdownGlob}...`); const results = await validateFiles(filesToValidate, validationOptions); // Print errors and optionally exit with a non-zero code if issues are found const hasErrors = printErrors(results, true); // true means exit with code 1 on errors if (hasErrors) { console.error('\nValidation completed with errors.'); } else { console.log('\nAll markdown links are valid!'); } } runValidation().catch(console.error);
next-validate-link --version
Debug
Known issues
breakingThe return type of the `validateFiles` function changed from `ValidateError[]` to `ValidateResult[]`. Developers should update their code to expect an array of `ValidateResult` objects, which now encapsulate errors alongside other file metadata.
fix
Update handlers for `validateFiles` to iterate over `ValidateResult[]` and access errors via `result.errors` property, rather than directly processing `ValidateError[]`.
affects: >=1.6.0
gotchaWhen validating relative URLs or paths, `next-validate-link` requires a clear understanding of your project's base URL and how file paths map to published URLs. Incorrect configuration can lead to false positives or missed broken links.
fix
Ensure `baseUrl`, `checkRelativePaths`, `checkRelativeUrls`, and potentially `pathToUrl` options are correctly configured to match your deployment environment and routing conventions. For Next.js, using the `next` preset in `scanURLs` is often sufficient.
affects: >=1.0.0
gotchaThe library heavily relies on file-system-based routing for frameworks like Next.js, Astro, and Nuxt. If your routing is highly dynamic or non-standard, the automatic URL scanning (`scanURLs`) might not detect all possible valid routes, leading to reported 'broken' links that are actually valid.
fix
For complex routing scenarios, you may need to manually provide an array of all valid `pages` to the `scanURLs` function or implement a custom `pathToUrl` resolver within `validateFiles` options.
affects: >=1.0.0
Errors
Common errors & fixes
Error: No files found matching pattern: 'content/**/*.md'
The glob pattern provided to `readFiles` or `validateFiles` does not match any existing files in the specified directory, or the path is incorrect.
fix
Verify the glob pattern and the starting directory. Ensure that the path segments (e.g., `docs`, `content`) correctly reflect your project's structure and that the file extensions (e.g., `md`, `mdx`) are correct. Use `path.join(process.cwd(), 'your-glob')` for robust path resolution.
TypeError: (0 , next_validate_link_1.validateFiles) is not a function
This error typically occurs when mixing CommonJS (`require`) with an ESM-first library or when the default/named import is incorrect. The library is primarily ESM.
fix
Ensure you are using `import { validateFiles } from 'next-validate-link';` and that your project is configured to handle ESM, especially if running directly with Node.js or older environments. If using `require`, you might need dynamic `import()` or adjust your build system.
[ERROR] Broken Link: Found 404 for /non-existent-page in file.md:10:15
A link within a Markdown file points to a path or URL that could not be resolved by the validator, either locally or externally.
fix
Check the reported link in the Markdown file (`file.md` at line 10, column 15). Verify if the target page or resource actually exists at the specified path/URL, or if the link is malformed. If it's a relative link, ensure your `baseUrl` and `pathToUrl` configurations are correct. If it's an external link, confirm network connectivity and the external resource's availability.
Upgrade
Version history
1.6.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
next-validate-link — npm install next-validate-link · libregistry