E: 无法找到npm软件包

131

当我尝试使用sudo apt-get install npm安装npm时,我会得到以下错误:

E: 无法定位软件包npm

为什么apt找不到npm? 我正在使用Debian 9,并已运行。

sudo apt-get install nodejs

3
这个问题可能更适合发布在superuser.com,因为它不是一个编程问题。 - Ulrich Eckhardt
10个回答

231

根据官方的Node.js文档:

Debian Sid(不稳定版)、Jessie(测试版)和Wheezy(wheezy-backports)的官方仓库中也提供了一个Node.js包,“nodejs”。它只安装了nodejs二进制文件。

因此,如果您只输入sudo apt-get install nodejs,它并不会安装其他好用的工具,如npm

您需要输入以下命令:

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

可选:安装构建工具

编译和安装npm中的本机附加组件,您可能还需要安装构建工具:

sudo apt-get install -y build-essential

更多信息: NodeJs文档


5
你有没有想法为什么 Debian 软件仓库中没有 npm? - martinkunev
1
当我运行以下命令时:curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -,我得到以下输出: + apt-get install -y lsb-release > /dev/null 2>&1 执行命令时出错,退出 - Levi
尝试了这个和许多其他有价值的建议后,我因为沮丧而关闭了终端。稍后我回来再次尝试,一开始就正常工作了。似乎每个人都忽略了的部分是在安装后打开和关闭终端,即重新启动它;) 我应该知道要尝试这个方法。 - Tyson Gibby
在Android 8上通过Termux安装Node.js时对我很有帮助。感谢您的帮助。 - AndyCODE

95

在Ubuntu for Windows中遇到这个问题,请先尝试运行以下命令

sudo apt-get update
sudo apt-get upgrade

然后

sudo apt-get install npm

2
这对我有用,该文章的“先决条件”部分已经确认:https://blogs.windows.com/buildingapps/2016/07/22/fun-with-the-windows-subsystem-for-linux/。它还建议执行`sudo apt upgrade`。 - Ambroise Rabier

37

我在Debian 9.2上遇到了同样的问题,以下是我采取的解决方法。

安装

sudo apt install curl
curl -sL https://deb.nodesource.com/setup_6.x | sudo bash -
sudo apt-get install -y nodejs
sudo apt-get install -y npm

检查已安装的版本

node --version
npm --version

本文翻译自"How to install Node.js LTS on Debian 9 stretch",原始链接:http://linuxbsdos.com/2017/06/26/how-to-install-node-js-lts-on-debian-9-stretch/


8
Debian官方没有安装npm的方式吗?npm说它已经包含在node.js里了,那我为什么在执行"apt install nodejs"命令时无法使用npm呢? - 0__
2
对我很有用!您可以使用 curl -sL https://deb.nodesource.com/setup_lts.x | sudo bash - 获取最新的 LTS 版本。 - Datz

15

由于您尚未将nodejs存储库添加到系统中,因此您的系统找不到npm软件包。

请按照以下安装步骤进行操作:
将nodejs PPA存储库和Python软件属性添加到我们的系统中。

sudo apt-get install curl python-software-properties 
// sudo apt-get install curl software-properties-common

curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
sudo apt-get update

然后安装npm

sudo apt-get install nodejs

检查是否已安装npm和node,并且您已准备好使用node.js

node -v
npm -v
如果有人安装NodeJS失败了,可以先尝试移除npm,可能是旧版本的安装出了问题。
sudo apt-get remove nodejs
sudo apt-get remove npm

检查是否存在npm或node文件夹,如果发现,请删除它们

which node
which npm

(我使用基于Debian Jessie的Bunsen-Helium系统)sudo apt-get install curl python-software-properties 对我无效,所以我不得不将其更改为:sudo apt-get install curl software-properties-common。 - AAAfarmclub
1
(它们现在已经升级到 10.x 版本了)$ curl -sL https://deb.nodesource.com/setup_10.x | sudo bash - - AAAfarmclub
感谢您的评论,我已经更新了答案@AAAfarmclub。 - StefansArya
在Ubuntu 20.04.1 LTS上安装Node和NPM成功。 - sg28

5
对于Debian Stretch(Debian 9版本),nodejs不包括npm,要安装它作为单独的软件包,您必须启用stretch-backports
echo 'deb http://deb.debian.org/debian stretch-backports main' |
sudo tee /etc/apt/sources.list.d/stretch-backports.list
apt-get update -y
apt-get -t stretch-backports install -y npm

在Buster(Debian 10)中,npm是一个常规软件包,因此从现在开始,这应该可以正常工作。但我们中的一些人在未来一段时间内仍将被困在Stretch版本上。

1
100%正确。如果您不确定是否在Debian Stretch上,请运行“lsb_release -a”,如果是的话,请按照此答案操作。 - Rub

4

这将解决您的错误。在终端中运行以下命令。这些命令将添加旧版本。您可以稍后更新它们,或者在逐个运行这些命令之前也可以在此更改版本。

sudo apt-get install build-essential
wget http://nodejs.org/dist/v0.8.16/node-v0.8.16.tar.gz
tar -xzf node-v0.8.16.tar.gz
cd node-v0.8.16/
./configure
make
sudo make install

谢谢,这条命令:'sudo apt-get install build-essential'解决了我的问题。 - Wes
从源代码安装意味着您将不会获得任何安全更新,除非您自己获取并构建新的本地版本,每当有新的补丁可用时。 - tripleee

2

在我的Jenkins/Jenkins Docker中,使用sudo总是会产生错误:

bash: sudo: command not found

我需要更新仓库列表:

curl -sL https://deb.nodesource.com/setup_10.x | apt-get update

那么,
 apt-get install nodejs

所有命令行结果都像这样:

root@76e6f92724d1:/# curl -sL https://deb.nodesource.com/setup_10.x | apt-get update
Ign:1 http://deb.debian.org/debian stretch InRelease
Get:2 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Get:3 http://deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:4 http://deb.debian.org/debian stretch Release [118 kB]
Get:5 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [520 kB]
Get:6 http://deb.debian.org/debian stretch-updates/main amd64 Packages [27.9 kB]
Get:8 http://deb.debian.org/debian stretch Release.gpg [2410 B]
Get:9 http://deb.debian.org/debian stretch/main amd64 Packages [7083 kB]
Get:7 https://packagecloud.io/github/git-lfs/debian stretch InRelease [23.2 kB]
Get:10 https://packagecloud.io/github/git-lfs/debian stretch/main amd64 Packages [4675 B]
Fetched 7965 kB in 20s (393 kB/s)
Reading package lists... Done
root@76e6f92724d1:/#  apt-get install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libicu57 libuv1
The following NEW packages will be installed:
  libicu57 libuv1 nodejs
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 11.2 MB of archives.
After this operation, 45.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian stretch/main amd64 libicu57 amd64 57.1-6+deb9u3 [7705 kB]
Get:2 http://deb.debian.org/debian stretch/main amd64 libuv1 amd64 1.9.1-3 [84.4 kB]
Get:3 http://deb.debian.org/debian stretch/main amd64 nodejs amd64 4.8.2~dfsg-1 [3440 kB]
Fetched 11.2 MB in 26s (418 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libicu57:amd64.
(Reading database ... 12488 files and directories currently installed.)
Preparing to unpack .../libicu57_57.1-6+deb9u3_amd64.deb ...
Unpacking libicu57:amd64 (57.1-6+deb9u3) ...
Selecting previously unselected package libuv1:amd64.
Preparing to unpack .../libuv1_1.9.1-3_amd64.deb ...
Unpacking libuv1:amd64 (1.9.1-3) ...
Selecting previously unselected package nodejs.
Preparing to unpack .../nodejs_4.8.2~dfsg-1_amd64.deb ...
Unpacking nodejs (4.8.2~dfsg-1) ...
Setting up libuv1:amd64 (1.9.1-3) ...
Setting up libicu57:amd64 (57.1-6+deb9u3) ...
Processing triggers for libc-bin (2.24-11+deb9u4) ...
Setting up nodejs (4.8.2~dfsg-1) ...
update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode

0

使用以下命令下载存储库密钥:

curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -

然后设置存储库:

sudo sh -c "echo deb https://deb.nodesource.com/node_8.x cosmic main \
                 > /etc/apt/sources.list.d/nodesource.list"
sudo apt-get update
sudo apt-get install nodejs

-1

我曾经遇到类似的问题,以下是解决方法。

添加 NodeSource 包签名密钥:

curl -sSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
# wget can also be used:
# wget --quiet -O - https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -

添加所需的NodeSource存储库:

# Replace with the branch of Node.js or io.js you want to install: node_6.x, node_12.x, etc...
VERSION=node_12.x
# The below command will set this correctly, but if lsb_release isn't available, you can set it manually:
# - For Debian distributions: jessie, sid, etc...
# - For Ubuntu distributions: xenial, bionic, etc...
# - For Debian or Ubuntu derived distributions your best option is to use the codename corresponding to the upstream release your distribution is based off. This is an advanced scenario and unsupported if your distribution is not listed as supported per earlier in this README.
DISTRO="$(lsb_release -s -c)"
echo "deb https://deb.nodesource.com/$VERSION $DISTRO main" | sudo tee /etc/apt/sources.list.d/nodesource.list
echo "deb-src https://deb.nodesource.com/$VERSION $DISTRO main" | sudo tee -a /etc/apt/sources.list.d/nodesource.list

更新软件包列表并安装 Node.js

sudo apt-get update
sudo apt-get install nodejs

-5

如果您已经安装了Node.js,那么您也有npm。Npm是随同Node.js一起安装的。


我刚刚偶然发现了这个问题 https://stackoverflow.com/q/47364736/3961881。它可能也能回答你的问题。 - diogo
无效链接。另外,Jacs 是正确的,npm 并没有与 node 一起安装,至少在 Debian 上不是这样。 - Display name

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接