简体   繁体   English

npm 错误。 cb.apply 不是 function

[英]npm ERR! cb.apply is not a function

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.在 Linux 进行npm install时,尽管我的 npm 版本是 6.9.0。 My node version is v12.18.3.我的节点版本是 v12.18.3。 How to resolve this issue?如何解决这个问题?

Would be helpful if you shared if it's Windows or Linux, but the error seems to occur on Windows.如果您共享它是 Windows 或 Linux,将会很有帮助,但错误似乎发生在 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.之后, npm install应该可以正常工作。

Mac/Macbook users Mac/Macbook 用户

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.由于这个问题相当活跃,并且这个问题的谷歌搜索结果最高(我知道 OP 使用的是 Linux),这里是如何在 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.在 Mac 上,您没有AppData\Roaming文件夹,因此只需运行npm root -g然后导航到隐藏目录并删除那里的node_modules If using nvm you'll need to rerun nvm use xxx (where xxx is your version).如果使用nvm ,您需要重新运行nvm use xxx (其中xxx是您的版本)。

Solution for Windows: 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.就我而言,我不想删除C:/users/me/AppData/Roaming/内的所有 npm 目录,因为我有许多已经存在并安装的全局模块。

What worked was inspired by @user14360499's answer:有效的灵感来自@user14360499 的回答:

  1. go inside C:\Users\me\AppData\Roaming\npm\node_modules go 内C:\Users\me\AppData\Roaming\npm\node_modules
  2. delete the npm dir删除npm目录
  3. npm install my project, and keep working as usually npm install我的项目,并像往常一样继续工作

Solution for ubuntu 18.04: 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和npm版本:

node -v
npm -v

If it says there is no such node/npm after you already install, then:如果在您安装后它说没有这样的节点/npm,那么:

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:如果您想更新 npm/node 稳定版本:

sudo npm install n -g
sudo n stable

Ciao, this problem could be connected to graceful-fs package. Ciao,这个问题可能与graceful-fs package有关。 You could try to reinstall it:您可以尝试重新安装它:

npm install graceful-fs --save-dev

I had the same issue with Nodist.我对 Nodist 也有同样的问题。 After changing my Node version, my npm version was actually incompatible with it.改了Node版本后,我的npm版本居然不兼容了。 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.因此,如果您使用的是 Nodist,请确保在切换 Node 版本后运行nodist npm {scope} match ,其中 scope 是全局、本地或环境。

Had the same error (macOS Big Sur) when i tried to use npx.当我尝试使用 npx 时出现同样的错误(macOS Big Sur)

How i fixed it (using nvm) when i run:我在运行时如何修复它(使用 nvm)

nvm ls

nvm ls output nvm ls output

Fix:使固定:

nvm alias default node
nvm install 'lts/*'

nvm install output非虚拟机安装 output

Solution for Ubuntu 18.04=> Ubuntu 18.04 的解决方案=>

  1. Visit 'usr/lib/node_modules'.访问“usr/lib/node_modules”。
  2. Delete 'n', 'npm','npm-clean','npm-install-peers'.删除“n”、“npm”、“npm-clean”、“npm-install-peers”。 That means delete all the node & npm related folders.这意味着删除所有节点和 npm 相关文件夹。
  3. Install the npm again.再次安装 npm。

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.我最终清理了 node、nodejs 和 npm 的所有痕迹。 May need some manual force remove after the purge call.清除呼叫后可能需要手动移除。 And then installed using nvm.然后使用 nvm 安装。

https://linuxize.com/post/how-to-install-node-js-on-ubuntu-20-04/#installing-nodejs-and-npm-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)它得到了nodejs v 15.4.0和npm 7.0.15的兼容版本(在使用apt-get之前它正在安装npm 6.9.0)

In my case it was indeed the graceful-fs package.就我而言,它确实是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.我之前也跑过nodist npm {scope} match但我不确定它是否有必要。

On macOS BigSur, I had this problem when executing:在 macOS BigSur 上,我在执行时遇到了这个问题:

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.运行这两个命令解决了这个问题。

I had this problem while trying to run npx .我在尝试运行npx时遇到了这个问题。

I found that nvm had been removed from the PATH.我发现nvm已从 PATH 中删除。

I added it back to my.zshrc file and it worked.我将它添加回 my.zshrc 文件并且它有效。

I was getting this error when running npx sb init .运行npx sb init时出现此错误。 For some reason, the command worked localy but not in CI.出于某种原因,该命令在本地工作,但在 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我按照这里的建议将 npx npx sb init替换为yarn global add @storybook/cli && yarn exec sb inithttps://github.com/yarnpkg/yarn/issues/3937#issuecomment-751289438

(NVM, Node.js 14, Yarn 1) (NVM, Node.js 14, 纱线 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.这意味着您的 npm 版本不正确。

reinstall npm, try again.重新安装npm,再试一次。

It works for me.这个对我有用。

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?在安装 npm 时,我在 Linux 中收到此错误“npm ERR.cb.apply is not a function”,尽管我的 npm 版本是 2.1.9.3。 How to resolve this issue?如何解决这个问题?

Feb 2021 fix: 2021 年 2 月修复:

npm install

And then commit the changes to package-lock.json .然后将更改提交到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)更多信息: https://help.heroku.com/ZV7S7D6T/why-is-my-node-build-is-suddenly-displaying-npm-err-cb-apply-is-not-a-function (这发生在我在heroku上)

For linux users:对于 linux 用户:

  1. uninstall NPM , to do it go to /usr/local/lib/node_modules and do:卸载 NPM ,将 go 卸载到/usr/local/lib/node_modules并执行以下操作:

sudo rm -rn npm npx sudo rm -rn npm npx

  1. Once uninstalled, re-instal it:卸载后重新安装

npm install npm安装

  1. Even after doing this, in a specific project when trying to install a specific npm package, you may get the following error:即使在这样做之后,在特定项目中尝试安装特定的 npm package 时,您可能会收到以下错误:

bash: /usr/local/bin/npm: No such file or directory bash: /usr/local/bin/npm: 没有那个文件或目录

If so, in the terminal just run:如果是这样,在终端中运行:

hash -r hash -r

  1. Now you can retry installation of your wanted npm package.现在您可以重试安装您想要的 npm package。

In my case (Mac) it was because I had npx installed alongside n (alternative to nvm ).在我的情况下(Mac),这是因为我在n旁边安装了nvm npx

$ which npx
/usr/local/bin/npx

I removed this exectutable and now:我删除了这个可执行文件,现在:

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

and it works now.现在可以了。

For me, the solution was to delete the node modules and bower components folder of the project.对我来说,解决方案是删除项目的节点模块和凉亭组件文件夹。

for the mac OS remove the node version and reinstalling was work for me.对于 mac OS,删除节点版本并重新安装对我有用。 I used n npm version manager.我使用了 n npm 版本管理器。

On windows, using nvm , i just installed a newer nodejs version (before 14.17.0, now 14.18.2)在 windows 上,使用nvm ,我刚刚安装了更新的 nodejs 版本(在 14.17.0 之前,现在是 14.18.2)

For me, on Centos 7, it was about removing graceful-ts used locally by npx .对我来说,在npx 7 上,它是关于删除 npx 在本地使用的graceful-ts 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.所以我删除了整个graceful-fs目录并再次运行 npx。 Everything worked.一切正常。

rm -fR /usr/local/lib/node_modules/npx/node_modules/npm/node_modules/graceful-fs

I'm on a Mac, and had exactly the same issue.我在Mac上,并且遇到了完全相同的问题。 Deleting the node modules seemed a little overkill for me, so I ran npm update and it solved the issue.删除节点模块对我来说似乎有点矫枉过正,所以我运行了npm update ,它解决了这个问题。 Please note I did not yet have any package.json or any modules on the repo I was working in.请注意,我正在使用的 repo 中还没有任何 package.json 或任何模块。

Windows 10:视窗 10:

When calling npx create-react-app my-app, i got following error:调用 npx create-react-app my-app 时,出现以下错误:

npm ERR. npm 错误。 cb.apply is not a function cb.apply 不是函数

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 npm 安装 create-react-app --save-dev
  3. npx create-react-app my-app npx create-react-app 我的应用程序

The problem has occured when the node and npm packages are not latest.当节点和 npm 包不是最新的时出现问题。

For me the problem is resolved when I completely removed npm and node, and made a clean installation.对我来说,当我完全删除 npm 和节点并进行全新安装时,问题就解决了。 I am using ubuntu 22 lts我正在使用 ubuntu 22 lts

Had this issue when creating a new react app using create-react-app .使用create-react-app新的 React 应用程序时出现此问题。 在此处输入图像描述

I was using nvm, so I just updated it to the latest LTS version:我正在使用 nvm,所以我只是将它更新到最新的 LTS 版本:

nvm install --lts 在此处输入图像描述

then switched to the newly installed lts version:然后切换到新安装的 lts 版本:

nvm use --lts 在此处输入图像描述

closed and reopened the terminal, reran create-react-app and the error was fixed!关闭并重新打开终端,重新运行create-react-app ,错误已修复!

Solution for ubuntu => ubuntu 解决方案 =>

  1. Completely remove nodejs and npm from your system.从系统中完全删除 nodejs 和 npm。
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.从此参考安装节点( https://computingforgeeks.com/install-node-js-14-on-ubuntu-debian-linux/ ) - 这将反过来安装最新的 npm。

Had the same error was simply in the wrong folder -.- (not a typescript project)有同样的错误只是在错误的文件夹中-.-(不是 typescript 项目)

So many solutions just made me even more puzzled.这么多的解决方案只是让我更加困惑。 I downloaded and installed the newest LTS NodeJS and it worked just fine.我下载并安装了最新的 LTS NodeJS,它运行良好。 https://nodejs.org/en/ https://nodejs.org/en/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM