Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 27, 2025

Problem

The Docusaurus build was failing with a ReferenceError: File is not defined error when using the @easyops-cn/docusaurus-search-local plugin. The error occurred in the undici library (a dependency of cheerio used by the search plugin) when trying to access the File Web API in a Node.js environment where it's not natively available.

Error: Docusaurus could not load module at path "/node_modules/@easyops-cn/docusaurus-search-local/dist/server/server/index.js"
Cause: File is not defined
    at /node_modules/undici/lib/web/webidl/index.js:512:48

Additionally, the build was failing due to numerous broken links in the documentation structure.

Solution

1. File API Polyfill

Added a minimal polyfill (polyfills.js) that provides File and Blob constructors for the Node.js environment. This polyfill is loaded in docusaurus.config.ts before other modules to ensure the Web APIs are available when the search plugin initializes.

// Simple File polyfill for Node.js
globalThis.File = class File {
  constructor(fileBits, fileName, options = {}) {
    this.name = fileName;
    this.type = options.type || '';
    // ... implementation
  }
  // ... methods
};

2. Documentation Structure

Created missing documentation index pages:

  • docs/Javascript/Advanced/index.md - Landing page for advanced JavaScript topics
  • docs/Javascript/Intermediate/index.md - Overview of intermediate concepts
  • docs/Javascript/Beginner/index.md - Beginner guide and learning path

3. Link Resolution

Fixed broken internal links throughout the documentation by:

  • Using absolute paths (/docs/...) instead of relative paths to avoid Docusaurus routing issues
  • Correcting malformed relative path references in markdown files
  • Ensuring all cross-references point to existing pages

Testing

  • ✅ Build completes successfully without errors
  • ✅ Search functionality works correctly
  • ✅ All internal navigation links function properly
  • ✅ No broken links detected with onBrokenLinks: "throw"

The fix is minimal and surgical - it only adds the necessary polyfill and creates the missing documentation structure without modifying any existing working code.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@sammy6378 sammy6378 marked this pull request as ready for review August 27, 2025 09:12
Copilot AI and others added 3 commits August 27, 2025 09:17
Co-authored-by: sammy6378 <143026733+sammy6378@users.noreply.github.com>
Co-authored-by: sammy6378 <143026733+sammy6378@users.noreply.github.com>
Co-authored-by: sammy6378 <143026733+sammy6378@users.noreply.github.com>
Copilot AI changed the title [WIP] fix error my-docs@1.0.0 build > docusaurus build [INFO] [en] Creating an optimized production build... Error: Error: Unable to build website for locale en. at tryToBuildLocale (/home/runner/work/reference/reference/node_modules/@docusaurus/core/... Fix "File is not defined" error in Docusaurus build with search plugin Aug 27, 2025
Copilot AI requested a review from sammy6378 August 27, 2025 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants