How to solve "cb.apply is not a function" error when using Gitbook

I often useGitbook, A small Node.js software used to generate e-books from a set of markdown files.

I use it in e-books. Today, I tried to generate a PDF and rungitbook pdf ., When I encountered a very strange error:

➜  ebook git:(master) ✗ gitbook pdf .
/usr/local/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js:287
      if (cb) cb.apply(this, arguments)
                 ^

TypeError: cb.apply is not a function at /usr/local/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js:287:18

cb.apply is not a function. What exactly does this mean? Most importantly, why do I get this errorright now? I haven't updated the gitbook package recently, nor...oh, I think I updated the version of Node.js I am running. But I don't know why this should be the problem. maybe.

Anyway... the error comes from/usr/local/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.jsfile. this isgraceful-jsnpm package, Is a "replacement" for the built-in Node.jsfsModules, various improvements", installed more than 33 million times a week.

Today, one of these improvements seems to have disrupted my workflow!

I don't have a lot of time to find out why my version of Node.js caused problems with this application and this library that I didn't create.

I opened the file/usr/local/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js, Where did the error come from.

This is the function that gives the problem:

function statFix (orig) {
  if (!orig) return orig
  // Older versions of Node erroneously returned signed integers for
  // uid + gid.
  return function (target, cb) {
    return orig.call(fs, target, function (er, stats) {
      if (!stats) return cb.apply(this, arguments)
      if (stats.uid < 0) stats.uid += 0x100000000
      if (stats.gid < 0) stats.gid += 0x100000000
      if (cb) cb.apply(this, arguments)
    })
  }
}

This seems to solve the problem in the old version of Node.js.

I see it used in lines 62-64 of the same file:

fs.stat = statFix(fs.stat)
fs.fstat = statFix(fs.fstat)
fs.lstat = statFix(fs.lstat)

I commented out the following lines:

// fs.stat = statFix(fs.stat)
// fs.fstat = statFix(fs.fstat)
// fs.lstat = statFix(fs.lstat)

And everything is ok, I am able to rungitbookOrder again and I got a good PDF.

Download mine for freeJavaScript beginner's manual


More js tutorials: