节点包管理器以某种方式损坏,现在无法安装

当我决定在nodejs app目录中使用npm update -g时,我遇到了一个问题。 这是一个糟糕的决定,因为它导致我遇到npm的常见和灾难性问题 。

解决方案是重新安装nodejs和npm。 不幸的是,当我使用sudo apt-get install npm它不起作用。

终端中的完整错误如下:

 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 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 (>= 2.1.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-which but it is not going to be installed E: Unable to correct problems, you have held broken packages. 

我该怎么做才能让npm正确安装并再次运行?

谢谢。

更新

关于William Entriken在这个答案下面的评论,有一种更好的方法可以在Ubuntu中本地安装Node.js作为快照包。

Node.js在所有当前支持的Ubuntu版本中都可以作为快照包使用。 特定于Node.js,开发人员可以从当前支持的版本中选择一个,并直接从NodeSource获得定期自动更新。 Node.js版本6,8和9目前可用,Snap Store在几小时或几分钟内更新Node.js。

可以使用单个命令安装节点,例如:

 sudo snap install node --classic --channel 9/stable 

命令node可以访问节点捕捉,例如:

  $ node -v  
 v9.9.0 

最新版本的npm将作为节点快照的一部分安装。 npm应该在普通shell中的节点repl之外运行。 安装节点快照后,运行以下命令以启用npm更新检查:

  sudo chown -R $ USER:$(id -gn $ USER)/ home / your-username /.config 

使用your-username替换上述命令中的用户名。 然后运行npm -v以检查npm -v的版本是否是最新的。 作为一个例子,我检查了npm是最新的,使用命令npm list yarn检查已安装的名为yarn的软件包的版本,然后使用命令npm update yarn将现有的纱线包更新到最新版本

用户可以随时在Node.js版本之间切换,而无需涉及其他工具,如nvm(节点版本管理器),例如:

 sudo snap refresh node --channel=8/stable 

用户可以测试最新版本的Node.js的最新版本,这些版本可以从最新的边缘通道安装,该通道当前通过切换来跟踪Node.js版本10的开发工作:

 sudo snap switch node --edge 

此方法仅建议那些愿意参与上游测试和错误报告的用户。


原始答案

要在所有当前支持的Ubuntu版本中安装Node.js(nodejs)和Node Manager的最新LTS版本,请打开终端并运行以下命令:

 sudo apt-get remove nodejs npm ## remove existing nodejs and npm packages sudo apt-get install curl curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - sudo apt-get install -y nodejs 

如果要复制这三个命令,请不要忽略第二个命令末尾的连字符。

nodejs包中包含nodejs binary和npm ,因此您无需单独安装npm 。 但是,为了使某些npm包能够工作(例如那些需要从源build-essential包),您需要安装build-essential包:

 sudo apt-get install build-essential 

LTS计划

Node.js的新semver主要版本每六个月从master中删除一次。 新的偶数版本(例如v6,v8,v10等)将于4月份上市。 10月份将删除新的奇数版本(例如v5,v7,v9)。

当一个新的奇数主要版本被削减时,之前的偶数主要版本将转换为长期支持计划。

LTS计划涵盖的每个主要版本将从其进入LTS覆盖之日起的18个月内积极维护。 在这18个月的积极支持之后,主要版本将进入“维护”模式再延长12个月。

非LTS安装

截至2017年3月,安装最新的非LTS版Node.js(发布时为v8)的命令如下:

 sudo apt-get remove nodejs npm ## remove existing nodejs and npm packages sudo apt-get install curl curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - sudo apt-get install -y nodejs 

Node.js LTS时间表

  释放LTS状态代号LTS开始维护开始维护结束
 6.x Active Boron 2016-10-18 2018年4月2018年4月
 7.x没有LTS              
 8.x活性炭2017年1月201日至2019年4月2019年12月
 9.x没有LTS              
 10.x Active Dubnium 2018年10月至2020年4月2020年4月2021年  
 11.x没有LTS 2019年6月