108

I am getting this error

npm ERR! cb.apply is not a function

in Linux while doing npm install although my npm version is 6.9.0. My node version is v12.18.3. How to resolve this issue?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Janaki
  • 1,083
  • 2
  • 6
  • 7
  • In my case, I had the "start" script run an `npx` command, removing `npx` and installing the dependency I was trying to run with `npx` solved the issue – Elitezen Aug 26 '22 at 19:25

30 Answers30

144

Would be helpful if you shared if it's Windows or Linux, but the error seems to occur on Windows. Possible solution:

1. Go to C:\Users(your username)\AppData\Roaming
2. Delete the npm folder (possibly back it up) and if there is one npm cache folder.
3. Run `npm cache clear --force` (--force is now required to clean cache)

After that, npm install should work fine.

Marek Piotrowski
  • 2,988
  • 3
  • 11
  • 16
  • 14
    On my Windows computer, it was `npm cache clean` (not `npm clean cache`). – Kalinda Pride Sep 28 '20 at 00:16
  • I removed the directories but I also had to remove environment variables associated with nodejs and some other resources as well. Just to add to the above solution, make sure "EVERYTHING" nodejs related is wiped out clean from the machine. – Dame Lyngdoh Mar 06 '21 at 08:34
  • 3
    Yeah, this method is correct but at last step you can Run "npm cache clean --force" to clean your cache. and run "npm cache verify" to verify if you successfully done your task. – Mani Malekizadeh Jun 16 '21 at 11:37
38

Mac/Macbook users

Since this question is fairly active and a top google result for this issue (I know the OP is using Linux), here is how to fix it on a Mac.

On a Mac you do not have the AppData\Roaming folder, so simply run npm root -g and then navigate to the hidden directory and delete the node_modules there. If using nvm you'll need to rerun nvm use x.x.x (where x.x.x is your node version).

maxshuty
  • 9,708
  • 13
  • 64
  • 77
23

Solution for Windows:

In my case I didn't want to delete all the npm directory inside C:/users/me/AppData/Roaming/ because I had many global modules that already existed and installed.

What worked was inspired by @user14360499's answer:

  1. go inside C:\Users\me\AppData\Roaming\npm\node_modules
  2. delete the npm dir
  3. npm install my project, and keep working as usually
maxshuty
  • 9,708
  • 13
  • 64
  • 77
adir abargil
  • 5,495
  • 3
  • 19
  • 29
16

Solution for ubuntu 18.04:

sudo apt-get remove nodejs
sudo apt-get remove npm
sudo rm /usr/local/bin/node
sudo rm /usr/local/bin/npm
sudo apt-get install nodejs
sudo apt-get install npm

Then try node and npm version:

node -v
npm -v

If it says there is no such node/npm after you already install, then:

sudo ln -s /usr/bin/node /usr/local/bin/node
sudo ln -s /usr/bin/npm /usr/local/bin/npm

Try this if the command works:

npm -v
node -v

In case when you want to update npm/node stable version:

sudo npm install n -g
sudo n stable
maxshuty
  • 9,708
  • 13
  • 64
  • 77
jacobkim
  • 1,043
  • 10
  • 20
  • Well, bless your little heart. I was getting the error when doing a `npx create-react-app my-app`, but I accidentally left out the `my-app` part (a serendipitous accident), and suddenly it said I needed to do `create-react-app@5`. At the `OK?` prompt, I typed "Y". Then, it installed create-react-app (which I didn't know existed). Then I followed your last two lines, `sudo npm install n -g1` and `sudo n stable`. After that, I successfully performed a `npx create-react-app my-app`. – TARKUS Jun 20 '23 at 12:13
9

I had the same issue with Nodist. After changing my Node version, my npm version was actually incompatible with it. So if you're using Nodist, make sure to run nodist npm {scope} match after switching Node versions, where scope is either global, local or env.

8

For me, on Centos 7, it was about removing graceful-ts used locally by npx. First, I've had to open log file which was mentioned in the error message:

npm ERR! cb.apply is not a function

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-04-04T07_45_27_182Z-debug.log

In that log file there was this path:

verbose stack     at /usr/local/lib/node_modules/npx/node_modules/npm/node_modules/graceful-fs/polyfills.js:287:18

So I removed whole graceful-fs directory and ran npx again. Everything worked.

rm -fR /usr/local/lib/node_modules/npx/node_modules/npm/node_modules/graceful-fs
Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
  • this helped under win 10 with scoop. just delete C:\Users\myuser\scoop\persist\nodejs\bin\node_modules\npx\node_modules\npm\node_modules\graceful-fs – Veas Nov 17 '22 at 09:40
6

Had the same error (macOS Big Sur) when i tried to use npx.

How i fixed it (using nvm) when i run:

nvm ls

nvm ls output

Fix:

nvm alias default node
nvm install 'lts/*'

nvm install output

4

Ciao, this problem could be connected to graceful-fs package. You could try to reinstall it:

npm install graceful-fs --save-dev
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
3

In my case it was indeed the graceful-fs package. Running

npm i -g --force graceful-fs 

fixed the issue. I also ran nodist npm {scope} match before but I'm unsure if it was neccessary.

Xintellion
  • 31
  • 1
  • 2
2

Solution for Ubuntu 18.04=>

  1. Visit 'usr/lib/node_modules'.
  2. Delete 'n', 'npm','npm-clean','npm-install-peers'. That means delete all the node & npm related folders.
  3. Install the npm again.
2

I have tried a lot of solutions from different posts and google pages and nothing worked for me.

I ended up cleaning all traces of node, nodejs, and npm. May need some manual force remove after the purge call. And then installed using nvm.

https://linuxize.com/post/how-to-install-node-js-on-ubuntu-20-04/#installing-nodejs-and-npm-using-nvm

And it resolved the issue for me. It got the nodejs v 15.4.0 and the compatible version of npm 7.0.15 ( before using apt-get it was installing npm 6.9.0)

2

On macOS BigSur, I had this problem when executing:

npx install-peerdeps --dev eslint-config-airbnb

To fix, I ran

brew update

after which I was prompted to run

brew upgrade

Running the two commands fixed the issue.

Vlad L
  • 1,544
  • 3
  • 6
  • 20
1

I had this problem while trying to run npx.

I found that nvm had been removed from the PATH.

I added it back to my .zshrc file and it worked.

Batch
  • 21
  • 2
1

In my case (Mac) it was because I had npx installed alongside n (alternative to nvm).

$ which npx
/usr/local/bin/npx

I removed this exectutable and now:

$ which npx
/Users/MY_USER/n/bin/npx

and it works now.

catchergeese
  • 664
  • 5
  • 9
1

I was getting this error when running npx sb init. For some reason, the command worked localy but not in CI. I tried most of the solutions from here. But only the following worked.

I replaced npx sb init with yarn global add @storybook/cli && yarn exec sb init as suggested here: https://github.com/yarnpkg/yarn/issues/3937#issuecomment-751289438

(NVM, Node.js 14, Yarn 1)

Leksat
  • 2,923
  • 1
  • 27
  • 26
1

if you are also getting this error:

1 warn npm npm does not support Node.js v16.2.0

that means that your npm version is not correct.

reinstall npm, try again.

It works for me.

ilikechocolate
  • 193
  • 1
  • 12
0

Feb 2021 fix:

npm install

And then commit the changes to package-lock.json. That should update all your dependencies, thus fixing this issue.

More info: https://help.heroku.com/ZV7S7D6T/why-is-my-node-build-is-suddenly-displaying-npm-err-cb-apply-is-not-a-function (this happened to me on heroku)

Roger Oba
  • 1,292
  • 14
  • 28
  • I had a fresh install of nvm, node and npm on windows. I tried to run `$ npx create-react-app myApp` This command totally fixed the issue for me, even though there was no package.json file in the local directory. – zipzit Jun 23 '21 at 07:35
  • Worked for me almost a year later on windows. I upgraded from v12 node LTS to v16 LTS, then suddenly `npx` stopped working. i tried deleting cache and clearing it, but nothing solved until i ran `npm install`, then boom works like magic – r3wt Feb 06 '22 at 19:30
0

For linux users:

  1. uninstall NPM, to do it go to /usr/local/lib/node_modules and do:

sudo rm -r n npm npx

  1. Once uninstalled, re-instal it:

npm install

  1. Even after doing this, in a specific project when trying to install a specific npm package, you may get the following error:

bash: /usr/local/bin/npm: No such file or directory

If so, in the terminal just run:

hash -r

  1. Now you can retry installation of your wanted npm package.
Ahmad MOUSSA
  • 2,729
  • 19
  • 31
0

For me , the solution was to delete the node modules and bower components folder of the project.

0

for the mac OS remove the node version and reinstalling was work for me. I used n npm version manager.

0

On windows, using nvm, i just installed a newer nodejs version (before 14.17.0, now 14.18.2)

Leonardo Pinto
  • 309
  • 3
  • 7
0

So many solutions just made me even more puzzled. I downloaded and installed the newest LTS NodeJS and it worked just fine. https://nodejs.org/en/

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 10 '21 at 14:12
0

I'm on a Mac, and had exactly the same issue. Deleting the node modules seemed a little overkill for me, so I ran npm update and it solved the issue! Please note I did not yet have any package.json or any modules on the repo I was working in.

jsantos42
  • 3
  • 3
0

Windows 10:

When calling npx create-react-app my-app, i got following error:

npm ERR! cb.apply is not a function

To fix it, i did the following:

  1. Go to the folder you want to create the app in.
  2. npm install create-react-app --save-dev
  3. npx create-react-app my-app
vali
  • 71
  • 5
0

The problem has occured when the node and npm packages are not latest.

For me the problem is resolved when I completely removed npm and node, and made a clean installation. I am using ubuntu 22 lts

  • to remove node and npm completely I have followed this answer
0

Had this issue when creating a new react app using create-react-app. enter image description here

I was using nvm, so I just updated it to the latest LTS version:

nvm install --lts enter image description here

then switched to the newly installed lts version:

nvm use --lts enter image description here

closed and reopened the terminal, reran create-react-app and the error was fixed!

Chal13W1zz
  • 41
  • 3
0

This is a conflict between npm <= 5 and newer versions of Node 12 and 14. The solution is to upgrade npm to >= 6, which is installed with Node 12 and Node 14 by default.

More details are described in this GitHub issue: https://github.com/nodejs/node/issues/34162#issuecomment-652575905

Take the following steps:

1.Make sure that you're running npm >= 6 on your local machine. (This comes by default with Node 12 and 14.)

2.Run npm install and check in any changes to the package-lock.json.

3.Remove the "npm" key from the "engines" in package.json. The buildpack will simply use the version of npm that comes with your set version of Node. For example, both Node 12.18.3 and 14.6.0 are installed with npm 6.14.6.

0

Fixing Issue on Mac

If you're facing a Node.js module problem on your Mac, follow these steps:

  1. Open Terminal.

  2. Get the global npm modules path with:

    npm root -g

  3. Delete the folder with:

    sudo rm -r /usr/local/lib/node_modules

  4. Test by installing a package:

    npm install

  5. If not fixed, install 'graceful-fs':

    npm install graceful-fs --save-dev

By following these steps, you can resolve the Node.js module issue on your Mac.

Remember: Use terminal commands with caution and understand their effects. Feel free to copy and paste this concise version into a markdown file for quick reference.

Habibur Rahman
  • 194
  • 2
  • 8
-1

Solution for ubuntu =>

  1. Completely remove nodejs and npm from your system.
sudo apt-get remove nodejs
sudo apt-get remove npm
  1. Install node from this reference (https://computingforgeeks.com/install-node-js-14-on-ubuntu-debian-linux/) - This will inturn install latest npm .
Shreya B
  • 305
  • 2
  • 14
-1

Had the same error was simply in the wrong folder -.- (not a typescript project)

Alex
  • 17
  • 5