Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitbook-cli install error TypeError: cb.apply is not a function inside graceful-fs

When executing any gitbook command in the terminal, the following error is thrown:

/home/travis/.nvm/versions/node/v12.18.3/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 /home/travis/.nvm/versions/node/v12.18.3/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js:287:18
    at FSReqCallback.oncomplete (fs.js:169:5)

NOTE: This does not happen in node version v12.18.2 and is still happening in version v14.6.0

The gitbook-cli seems to not be working right now, failing because of their outdated graceful-fs dependency.

Any ideas how to fix this?

like image 631
Ariel Mirra Avatar asked Oct 05 '20 15:10

Ariel Mirra


People also ask

How do you fix NPM err CB apply is not a function?

Delete the roaming npm folder on Windows I found the npm version stays while node has been updated. Once you removed the npm folder in Roaming , restart your command line. You should see the latest npm version printed by running the npm -v command. Now the npm install command should work.


4 Answers

The issue was originally a problem inside graceful-fs but they solved it in this commit I believe.

The problem is that GitBook is still using outdated dependencies that pull in versions of graceful-fs without the fix.

The solution I found was to update graceful-fs inside gitbook like this:

If you've installed gitbook globally by doing npm install -g gitbook-cli then your path in macOS should be something like /usr/local/lib/node_modules/gitbook-cli/node_modules/npm/node_modules. Your path may differ depending on your OS or installation location.

Run this:

cd /usr/local/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/
npm install graceful-fs@latest --save

Then try installing GitBook. It should have fixed the installing problem.

like image 83
Ariel Mirra Avatar answered Oct 22 '22 20:10

Ariel Mirra


TL;DR

Why not switch to honkit(Fork of GitBook)?


The "why" for switching:

I knew this is not the answer to the question, but I would like to mention that the repository of gitbook has been deprecated 3 years ago, this makes it a bad choice for usage and especially security.

So I searched for the alternatives, and I found mdBook and honkit, turned out that honkit has good compatibility with gitbook, maybe switch to honkit is a better choice instead of fixing a 3 years old software with some hacking.

like image 30
Windsting Avatar answered Oct 22 '22 19:10

Windsting


The last solution was great , but when I use npm install graceful-fs@latest --save in the right place and then rerun gitbook build , I got the following error:

_stream_readable.js:545
  switch (state.pipesCount) {
               ^
TypeError: Cannot read property 'pipesCount' of undefined
    at module.exports.Readable.pipe (_stream_readable.js:545:16)
    at module.exports.ZipArchiveOutputStream._smartStream (/node_modules/compress-commons/lib/archivers/zip/zip-archive-output-stream.js:184:11)
    at module.exports.ZipArchiveOutputStream._appendStream (/node_modules/compress-commons/lib/archivers/zip/zip-archive-output-stream.js:96:20)
    at module.exports.ArchiveOutputStream.entry (/node_modules/compress-commons/lib/archivers/archive-output-stream.js:88:10)
    at module.exports.ZipStream.entry (/node_modules/zip-stream/index.js:138:49)
    at Zip.append (/node_modules/archiver/lib/plugins/zip.js:53:15)
    at Archiver._moduleAppend (/node_modules/archiver/lib/core.js:172:16)
    at Archiver._onQueueTask (/node_modules/archiver/lib/core.js:370:8)
    at /node_modules/async/dist/async.js:3879:9

And ... guess what , when I change the version of graceful-fs to 4.2.0 , it all worked ...

like image 11
mythsman Avatar answered Oct 22 '22 20:10

mythsman


Just replace the crashing file polyfills.js with this one: https://raw.githubusercontent.com/isaacs/node-graceful-fs/168bdb8f0bb3174e8499d4bc5878deead4172c39/polyfills.js

This works like a charm in Node.js v16.13.0.

like image 3
Svetlin Nakov Avatar answered Oct 22 '22 20:10

Svetlin Nakov