You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用broken-link-checker报错:SiteChecker不是构造函数

问题描述

1. SiteChecker is not a function 错误

配置并执行broken-link-checker包时触发该错误,代码如下:

var {SiteChecker} = require('broken-link-checker')

const siteChecker =  new SiteChecker({
    excludeExternalLinks: false,
    excludeExternalLinks:false,
    filterLevel: 0,
    acceptedSchemes: ['http', 'https'],
    excludeLinksToSamePage:true
},
{
// Define events for SiteChecker 
"error":(error)=>{
console.log(error)
},
"end":()=>{
    console.log("--------------------------")
    console.log("----THE SCAN IS DONE!-----")
    console.log("--------------------------")

},
"link":(result)=>{
  if ( result.broken )
    if( result.http.response && ![undefined,401,403].includes(result.http.response.statusCode) ){
  console.log(`${result.http.response.statusCode} => ${result.url.original}`);
    }
          
}

}

)

siteChecker.enqueue("https://amazon.com")

发现@types/broken-link-checker的index.d.ts文件路径为/AppData/Local/Microsoft/TypeScript/5.8/node_modules/@types/broken-link-checker/index,且它是索引文件而非类文件,想了解这意味着什么?该文件内容如下:

export interface Options {
    acceptedSchemes?: string[] | undefined;
    cacheExpiryTime?: number | undefined;
    cacheResponses?: boolean | undefined;
    excludedKeywords?: string[] | undefined;
    excludedSchemes?: string[] | undefined;
    excludeExternalLinks?: boolean | undefined;
    excludeInternalLinks?: boolean | undefined;
    excludeLinksToSamePage?: boolean | undefined;
    filterLevel?: 0 | 1 | 2 | 3 | undefined;
    honorRobotExclusions?: boolean | undefined;
    maxSockets?: number | undefined;
    maxSocketsPerHost?: number | undefined;
    rateLimit?: number | undefined;
    requestMethod?: string | undefined;
    retry405Head?: boolean | undefined;
    userAgent?: string | undefined;
}
/** Declaration file generated by dts-gen */

export class HtmlChecker {
    constructor(
        options: Options,
        handlers: {
            html?: ((tree: any, robots: any) => void) | undefined;
            junk?: ((result: any) => void) | undefined;
            link?: ((result: any) => void) | undefined;
            complete?: (() => void) | undefined;
        },
    );

    clearCache(): any;

    numActiveLinks(): any;

    numQueuedLinks(): any;

    pause(): any;

    resume(): any;

    scan(html: any, baseUrl: any, robots: any): any;
}

export class HtmlUrlChecker {
    constructor(
        options: Options,
        handlers: {
            html?: ((tree: any, robots: any) => void) | undefined;
            junk?: ((result: any) => void) | undefined;
            link?: ((result: any) => void) | undefined;
            page?: ((error: any, pageUrl: any, customData: any) => void) | undefined;
            end?: (() => void) | undefined;
        },
    );

    clearCache(): any;

    dequeue(id: any): any;

    enqueue(pageUrl: any, customData: any): any;

    numActiveLinks(): any;

    numPages(): any;

    numQueuedLinks(): any;

    pause(): any;

    resume(): any;
}

export class SiteChecker {
    constructor(
        options: Options,
        handlers: {
            robots?: ((robots: any, customData: any) => void) | undefined;
            html?: ((tree: any, robots: any) => void) | undefined;
            junk?: ((result: any) => void) | undefined;
            link?: ((result: any) => void) | undefined;
            page?: ((error: any, pageUrl: any, customData: any) => void) | undefined;
            site?: ((error: any, siteUrl: any, customData: any) => void) | undefined;
            end?: (() => void) | undefined;
        },
    );

    clearCache(): any;

    dequeue(id: any): any;

    enqueue(firstPageUrl: any, customData: any): any;

    numActiveLinks(): any;

    numPages(): any;

    numQueuedLinks(): any;

    numSites(): any;

    pause(): any;

    resume(): any;
}
}
解决方案与解释

1. 修复 SiteChecker is not a function 错误

错误根源是模块导入方式不兼容,broken-link-checker的CommonJS导出结构不支持直接解构导入,修改导入方式即可解决:

方法一(CommonJS):

const blc = require('broken-link-checker');
const SiteChecker = blc.SiteChecker;

方法二(ES模块):

如果你的项目使用import语法,可直接写:

import { SiteChecker } from 'broken-link-checker';

另外建议删除代码中重复的excludeExternalLinks: false配置,避免冗余。

2. 关于类型索引文件的解释

  • 该路径说明这是TypeScript自动安装的全局类型缓存文件,存放在TypeScript的本地缓存目录,而非你项目的node_modules下。当TypeScript找不到项目内的类型定义时,会自动从全局缓存中查找匹配的类型包。
  • index.d.ts是TypeScript的标准入口文件,作用是汇总该包对外暴露的所有类型、接口和类,本身不需要包含可执行代码,所以它不是“类文件”是完全正常的——类型定义文件仅用于给JavaScript库提供类型提示。
  • 文件顶部的注释表明这个类型文件是自动生成的,并非官方维护,可能存在和实际库版本不匹配的情况,这也可能是导入出错的潜在诱因,建议确保broken-link-checker和@types/broken-link-checker的版本兼容。

内容的提问来源于stack exchange,提问作者Nina Mishchenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 14:14:55