stevenvachon / broken-link-checker

Find broken links, missing images, etc within your HTML.
MIT License
1.95k stars 302 forks source link

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

Closed thomaspaulin closed 3 years ago

thomaspaulin commented 3 years ago

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);


4. Run `node index.js`
5. 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
matthamil commented 3 years ago

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)
thomaspaulin commented 3 years ago

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

stevenvachon commented 3 years ago

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.