无法安装npm - 无法解决问题,您有损坏的软件包?

我正在尝试升级我的Node.js到7.6版本,但失败了。现在,在重新安装Node 6.10后,我遇到了一个新问题:

$ sudo apt-get purge nodejs npm
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'npm' is not installed, so not removed
The following packages were automatically installed and are no longer required:
  linux-headers-4.8.0-40 linux-headers-4.8.0-40-generic linux-image-4.8.0-40-generic linux-image-extra-4.8.0-40-generic
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED
  nodejs*
0 to upgrade, 0 to newly install, 1 to remove and 53 not to upgrade.
After this operation, 51.7 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 278133 files and directories currently installed.)
Removing nodejs (6.9.5-1nodesource1~yakkety1) ...
Processing triggers for man-db (2.7.5-1) ...
(Reading database ... 274476 files and directories currently installed.)
Removing nodejs (6.9.5-1nodesource1~yakkety1) ...
Purging configuration files for nodejs (6.9.5-1nodesource1~yakkety1) ...
dpkg: warning: while removing nodejs, directory '/usr/lib/node_modules' not empty so not removed

为什么会显示“未安装'npm'包,因此无法删除”?我一直在使用npm来安装许多节点包用于我的项目!!
我忽略了这个错误并继续安装Node 7.6:
$ curl -sL https://deb.nodesource.com/setup_7.6 | sudo -E bash - 
bash: line 1: html: No such file or directory
bash: line 2: syntax error near unexpected token `<'
'ash: line 2: `<head><title>404 Not Found</title></head>

所以我回到了6.x版本的Node。但是我无法再安装npm了。
$ sudo apt-get install npm
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies.
 npm : Depends: nodejs but it is not going to be installed
       Depends: node-abbrev (>= 1.0.4) but it is not going to be installed
       Depends: node-ansi (>= 0.3.0-2) but it is not going to be installed
       Depends: node-ansi-color-table but it is not going to be installed
       Depends: node-archy but it is not going to be installed
       Depends: node-block-stream but it is not going to be installed
       Depends: node-fstream (>= 0.1.22) but it is not going to be installed
       Depends: node-fstream-ignore but it is not going to be installed
       Depends: node-github-url-from-git but it is not going to be installed
       Depends: node-glob (>= 3.1.21) but it is not going to be installed
       Depends: node-graceful-fs (>= 2.0.0) but it is not going to be installed
       Depends: node-inherits but it is not going to be installed
       Depends: node-ini (>= 1.1.0) but it is not going to be installed
       Depends: node-lockfile but it is not going to be installed
       Depends: node-lru-cache (>= 2.3.0) but it is not going to be installed
       Depends: node-minimatch (>= 0.2.11) but it is not going to be installed
       Depends: node-mkdirp (>= 0.3.3) but it is not going to be installed
       Depends: node-gyp (>= 0.10.9) but it is not going to be installed
       Depends: node-nopt (>= 3.0.1) but it is not going to be installed
       Depends: node-npmlog but it is not going to be installed
       Depends: node-once but it is not going to be installed
       Depends: node-osenv but it is not going to be installed
       Depends: node-read but it is not going to be installed
       Depends: node-read-package-json (>= 1.1.0) but it is not going to be installed
       Depends: node-request (>= 2.25.0) but it is not going to be installed
       Depends: node-retry but it is not going to be installed
       Depends: node-rimraf (>= 2.2.2) but it is not going to be installed
       Depends: node-semver (>= 2.1.0) but it is not going to be installed
       Depends: node-sha but it is not going to be installed
       Depends: node-slide but it is not going to be installed
       Depends: node-tar (>= 0.1.18) but it is not going to be installed
       Depends: node-underscore but it is not going to be installed
       Depends: node-which but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

有什么想法吗?

1NodeSource的Node.js包含了npm,所以不需要单独安装它。 - Firsh - justifiedgrid.com
5个回答

我曾经也遇到过npm的相同问题。我建议您安装aptitude,它可以帮助您解决冲突。
sudo apt install aptitude

然后使用aptitude安装您的软件包。
sudo aptitude install npm

我在使用npm时遇到了同样的问题,不得不按照以下顺序安装了这些软件包:
sudo apt install libssl1.0-dev
sudo apt install nodejs-dev
sudo apt install node-gyp
sudo apt install npm

希望这能帮到你。

1(在Kubuntu 18.04上)我之前安装了libssl-dev,结果发现对应的是libssl1.1,但是npm明显需要1.0版本。我不得不移除libssl-dev并用libssl1.0-dev替换,然后一切正常工作了。apt错误没有提供有用的信息来确定问题所在,所以你的答案真是救了我一命。 - Richard Hussong

sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node* ~/.npm
sudo rm -rf /usr/local/lib/node*
sudo rm -rf /usr/local/bin/node*
sudo rm -rf /usr/local/include/node*

sudo apt-get purge nodejs npm
sudo apt autoremove

然后安装Node和npm

这对我有效:
sudo apt install aptitude
sudo aptitude install libnode-dev                        
sudo aptitude install libnode64
sudo aptitude install node-gyp
sudo aptitude install npm

你在从nodesource安装nodejs时不需要安装npm,因为它已经包含在内。
你遇到的错误是因为npm包与内置的nodejs包绑定在一起。
只需执行以下操作:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install nodejs

它将安装nodenpm
node -v
npm -v

安装node,但不安装npm - Gathide

  • 相关问题