stevenvachon/broken-link-checker

"TypeError: (intermediate value).on is not a function" when trying to use SiteChecker

thomaspaulin opened this issue · 3 comments

Describe the bug
on(...) is not a function on the SiteChecker object.

To Reproduce

  1. Set up a Node project
  2. Install broken-link-checker with the npm command listed on the project readme - npm install broken-link-checker
  3. Copy the code snippet for a SiteChecker from the readme into index.js.
const {SiteChecker} = require('broken-link-checker');

const siteChecker = new SiteChecker({})
    .on('error', (error) => {})
    .on('robots', (robots, customData) => {})
    .on('html', (tree, robots, response, pageURL, customData) => {})
    .on('queue', () => {})
    .on('junk', (result, customData) => {})
    .on('link', (result, customData) => {})
    .on('page', (error, pageURL, customData) => {})
    .on('site', (error, siteURL, customData) => {})
    .on('end', () => {});

siteChecker.enqueue("https://www.google.com", undefined);
  1. Run node index.js
  2. Notice the error message

Expected behavior
I did not expect this error. I expected the readme code to work.

Environment:

  • OS and version: macOS Mojave (14.10.6)
  • Node.js version: v14.15.3
  • broken-link-checker version: ^0.7.8

I ran into the same issue. You don't call a .on method, but pass a second argument to the constructor to set these callbacks:

const handlers = {
  error: (error) => {},
  robots: (robots, customData) => {},
  // ...
}

const siteChecker = new SiteChecker(options, handlers)

Thanks @matthamil that work around...works! It doesn't fix the wrong documentation though.

Thanks @matthamil that work around...works! It doesn't fix the wrong documentation though.

Not if you use the published npm readme instead of the git trunk.