node.js 无法在 Ubuntu 中使用节点包管理器安装包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21168141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:40:31  来源:igfitidea点击:

Cannot install packages using node package manager in Ubuntu

node.jsubuntunpm

提问by Sayem

NodeJS interpreter name(node) on Ubuntu has been renamed to nodejsbecause of a name conflict with another package. Here's what the readme. Debian says:

由于与另一个包的名称冲突,nodeUbuntu 上的NodeJS 解释器名称( ) 已重命名为nodejs。这是自述文件。Debian 说:

The upstream name for the Node.js interpreter command is "node". In Debian the interpreter command has been changed to "nodejs".

This was done to prevent a namespace collision: other commands use the same name in their upstream, such as ax25-node from the "node" package.

Scripts calling Node.js as a shell command must be changed to instead use the "nodejs" command.

Node.js 解释器命令的上游名称是“node”。在 Debian 中,解释器命令已更改为“nodejs”。

这样做是为了防止命名空间冲突:其他命令在其上游使用相同的名称,例如“node”包中的 ax25-node。

必须将调用 Node.js 作为 shell 命令的脚本更改为使用“nodejs”命令。

However, using nodejs mucks up installing packages using npm. Package installation fails with the following error:

但是,使用 nodejs 会使使用npm. 软件包安装失败并出现以下错误:

sh: 1: node: not found
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read /usr/share/doc/nodejs/README.Debian
sh: 1: node: not found
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read /usr/share/doc/nodejs/README.Debian

How do I make npm understand that nodejs is already installed on the system but the interpreter name is different?

如何让 npm 了解系统上已经安装了 nodejs 但解释器名称不同?

回答by vbo

TL;DR:

特尔;博士:

sudo apt-get install nodejs-legacy


First of all let me clarify the situation a bit. In summer 2012 Debian maintainers decided to rename Node.js executable to prevent some kind of namespace collision with another package. It was very hard decision for Debian Technical Committee, because it breaks backward compatibility.

首先让我澄清一下情况。2012 年夏天,Debian 维护者决定重命名 Node.js 可执行文件,以防止与另一个包发生某种命名空间冲突。对于 Debian 技术委员会来说,这是一个非常艰难的决定,因为它破坏了向后兼容性。

The following is a quote from Committee resolution draft, published in Debian mailing list:

以下是委员会决议草案的引述,发表在 Debian邮件列表中

  1. The nodejs package shall be changed to provide /usr/bin/nodejs, not /usr/bin/node. The package should declare a Breaks: relationship with any packages in Debian that reference /usr/bin/node.

  2. The nodejs source package shall also provide a nodejs-legacy binary package at Priority: extra that contains /usr/bin/node as a symlink to /usr/bin/nodejs. No package in the archive may depend on or recommend the nodejs-legacy package, which is provided solely for upstream
    compatibility. This package declares shall also declare a Conflicts: relationship with the node package.

<...>

  1. nodejs 包应更改为提供 /usr/bin/nodejs,而不是 /usr/bin/node。该软件包应声明与 Debian 中引用 /usr/bin/node 的任何软件包的 Breaks: 关系。

  2. nodejs 源包还应在 Priority: extra 处提供 nodejs-legacy 二进制包,其中包含 /usr/bin/node 作为 /usr/bin/nodejs 的符号链接。存档中的任何包都不得依赖或推荐 nodejs-legacy 包,该包仅用于上游
    兼容性。该包声明还应声明一个 Conflicts: 与节点包的关系。

<...>

Paragraph 2 is the actual solution for OP's issue. OP should try to install this package instead of doing symlink by hand. Here is a link to this package in Debian package index website.

第 2 段是 OP 问题的实际解决方案。OP 应该尝试安装这个包,而不是手动进行符号链接。这是 Debian 软件包索引网站中该软件包的链接。

It can be installed using sudo apt-get install nodejs-legacy.

可以使用sudo apt-get install nodejs-legacy.

I have not found any information about adopting the whole thing by NPM developers, but I think npmpackage will be fixed on some point and nodejs-legacybecome really legacy.

我还没有找到任何关于 NPM 开发人员采用整个东西的信息,但我认为npm包将在某个时候修复并nodejs-legacy成为真正的遗产。

回答by leorex

Try linking node to nodejs. First find out where nodejs is

尝试将节点链接到 nodejs。首先找出nodejs在哪里

whereis nodejs

Then soft link node to nodejs

然后软链接节点到nodejs

ln -s [the path of nodejs] /usr/bin/node 

I am assuming /usr/bin is in your execution path. Then you can test by typing node or npm into your command line, and everything should work now.

我假设 /usr/bin 在您的执行路径中。然后您可以通过在命令行中输入 node 或 npm 来进行测试,现在一切都应该可以正常工作了。

回答by Eduardo Dennis

You can also install Nodejs using NVMor Nodejs Version ManagerThere are a lot of benefitsto using a version manager. One of them being you don't have to worry about this issue.

您还可以使用NVMNodejs 版本管理器安装 Nodejs 使用版本管理器有很多好处。其中之一是您不必担心这个问题。



Instructions:

指示:



sudo apt-get update
sudo apt-get install build-essential libssl-dev

Once the prerequisite packages are installed, you can pull down the nvm installation script from the project's GitHub page. The version number may be different, but in general, you can download and install it with the following syntax:

安装必备软件包后,您可以从项目的 GitHub 页面下拉 nvm 安装脚本。版本号可能不同,但一般情况下,您可以使用以下语法下载并安装:

curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh

This will download the script and run it. It will install the software into a subdirectory of your home directory at ~/.nvm. It will also add the necessary lines to your ~/.profilefile to use the file.

这将下载脚本并运行它。它会将软件安装到您的主目录的子目录中~/.nvm。它还会将必要的行添加到您的~/.profile文件中以使用该文件。

To gain access to the nvm functionality, you'll need to log out and log back in again, or you can source the ~/.profile file so that your current session knows about the changes:

要访问 nvm 功能,您需要注销并重新登录,或者您可以获取 ~/.profile 文件,以便您的当前会话了解更改:

source ~/.profile

Now that you have nvm installed, you can install isolated Node.js versions.

现在您已经安装了 nvm,您可以安装独立的 Node.js 版本。

To find out the versions of Node.js that are available for installation, you can type:

要找出可供安装的 Node.js 版本,您可以键入:

nvm ls-remote
. . .

v0.11.10
v0.11.11
v0.11.12
v0.11.13
v0.11.14

As you can see, the newest version at the time of this writing is v0.11.14. You can install that by typing:

如您所见,撰写本文时的最新版本是 v0.11.14。您可以通过键入以下内容进行安装:

nvm install 0.11.14

Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:

通常,nvm 会切换到使用最近安装的版本。您可以通过键入以下内容明确告诉 nvm 使用我们刚刚下载的版本:

nvm use 0.11.14

When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:

使用 nvm 安装 Node.js 时,可执行文件称为 node。您可以通过键入以下内容查看 shell 当前正在使用的版本:

node -v

The comeplete tutorial can be found here

可以在这里找到完整的教程

回答by Ritesh

  1. Install nvmfirst using:

    curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
    
  2. Run command

    source ~/.profile
    
  3. Now run this and this will show will all installed or other versions of packages:

    nvm ls-remote
    
  4. Installed packages will be in green. Install whatever version you want:

    nvm install 6.0.0
    
  5. Check where is not installed:

    which node
    
  6. Check current version:

    node -v
    
    n=$(which node);
    n=${n%/bin/node}; 
    chmod -R 755 $n/bin/*; 
    sudo cp -r $n/{bin,lib,share} /usr/local
    
  1. nvm首先安装使用:

    curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
    
  2. 运行命令

    source ~/.profile
    
  3. 现在运行它,这将显示所有已安装或其他版本的软件包:

    nvm ls-remote
    
  4. 已安装的软件包将以绿色显示。安装任何你想要的版本:

    nvm install 6.0.0
    
  5. 检查没有安装的地方:

    which node
    
  6. 检查当前版本:

    node -v
    
    n=$(which node);
    n=${n%/bin/node}; 
    chmod -R 755 $n/bin/*; 
    sudo cp -r $n/{bin,lib,share} /usr/local
    

回答by Xiao Peng - ZenUML.com

sudo apt-get --purge remove node
sudo apt-get --purge remove nodejs-legacy
sudo apt-get --purge remove nodejs

sudo apt-get install nodejs-legacy
source ~/.profile

Combined the accepted answer with source ~/.profilefrom the comment that has been folded and some clean up commands before. Most likely you will also need to sudo apt-get install npmafter.

将接受的答案与source ~/.profile之前已折叠的评论和一些清理命令结合起来。很可能你也需要sudo apt-get install npm之后。

回答by shrishinde

for me problem was solved by,

对我来说,问题解决了,

sudo apt-get remove node
sudo apt-get remove nodejs
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo ln -s /usr/bin/nodejs /usr/bin/node
alias node=nodejs
rm -r /usr/local/lib/python2.7/dist-packages/localstack/node_modules
npm install -g npm@latest || sudo npm install -g npm@latest

回答by Ville

Here's another approach I use since I like nfor easy switching between node versions.

这是我使用的另一种方法,因为我喜欢n以便在节点版本之间轻松切换。

On a new Ubuntu system, first install the 'system' node:

在新的 Ubuntu 系统上,首先安装 'system' 节点:

curl -sL https://deb.nodesource.com/setup | sudo bash -

Then install nmodule globally:

然后全局安装n模块:

npm install -g n

Since the system node was installed first (above), the alternatives system can be used to cleanly point to the node provided by n. First make sure the alternatives system has nothing for node:

由于首先安装了 system 节点(上图),因此可以使用替代系统干净地指向n提供的节点。首先确保替代系统没有节点:

update-alternatives --remove-all node

Then add the node provided by n:

然后添加n提供的节点:

update-alternatives --install /usr/bin/node node /usr/local/bin/node 1

Next add node provided by the system (the one that was installed with curl):

接下来添加系统提供的节点(使用 curl 安装的节点):

update-alternatives --install /usr/bin/node node /usr/bin/nodejs 2

Now select the node provided by nusing the interactive menu (select /usr/local/bin/nodefrom the menu presented by the following command):

现在使用交互式菜单选择n提供的节点(/usr/local/bin/node从以下命令提供的菜单中选择):

update-alternatives --config node

Finally, since /usr/local/binusually has a higher precedence in PATH than /usr/bin, the following alias must be created (enter in your .bashrc or .zshrc) if the alternatives system node is to be effective; otherwise the node installed with nin /usr/local/bin takes always precedence:

最后,由于/usr/local/bin通常在 PATH 中具有比 更高的优先级,/usr/bin如果要使替代系统节点有效,则必须创建以下别名(在您的 .bashrc 或 .zshrc 中输入);否则安装的节点ñ在/ usr / local / bin中需要始终优先:

alias node='/usr/bin/node'

Now you can easily switch between node versions with n <desired node version number>.

现在,您可以使用n <desired node version number>.

回答by xdeepakv

As other folks already mention, I will suggest not to use "sudo apt-get" to install node or any development library. You can download required version from https://nodejs.org/dist/v6.9.2/and setup you own environment.

正如其他人已经提到的,我建议不要使用“sudo apt-get”来安装节点或任何开发库。您可以从https://nodejs.org/dist/v6.9.2/下载所需的版本并设置您自己的环境。

I will recommend tools like nvmand n, to manage you node version. It is very convenient to switch and work with these modules. https://github.com/creationix/nvmhttps://github.com/tj/n

我会推荐像nvmn这样的工具来管理你的节点版本。切换和使用这些模块非常方便。 https://github.com/creationix/nvm https://github.com/tj/n

Or write basic bash to download zip/tar, extract move folder and create a soft link. Whenever you need to update, just point the old soft link to new downloaded version. Like I have created for my own, you can refer: https://github.com/deepakshrma/NodeJs-4.0-Reference-Guide/blob/master/nodejs-installer.sh

或者编写基本的 bash 来下载 zip/tar,解压移动文件夹并创建一个软链接。每当您需要更新时,只需将旧软链接指向新下载的版本即可。就像我为自己创建的一样,您可以参考:https: //github.com/deepakshrma/NodeJs-4.0-Reference-Guide/blob/master/nodejs-installer.sh

#Go to home
cd ~
#run command
#New Script
wget https://raw.githubusercontent.com/deepakshrma/NodeJs-4.0-Reference-Guide/master/nodejs-installer.sh 
bash nodejs-installer.sh -v lts
#here -v or --version can be sepecific to 0.10.37 or it could be latest/lts 
#Examples
bash nodejs-installer.sh -v lts
bash nodejs-installer.sh -v latest
bash nodejs-installer.sh -v 4.4.2

回答by Suzana

On Linux Mint 17, I tried both solutions (creating a symlink or using the nodejs-legacypackage) without success.

在 Linux Mint 17 上,我尝试了两种解决方案(创建符号链接或使用nodejs-legacy包),但都没有成功。

The only thing that finally worked for me was using the ppa from Chris Lea:

最终对我有用的唯一方法是使用 Chris Lea 的 ppa:

sudo apt-get purge node-*
sudo apt-get autoremove 
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

This installed node version 10.37 and npm 1.4.28. After that, I could install packages globally.

这安装了节点版本 10.37 和 npm 1.4.28。之后,我可以全局安装软件包。

回答by Krzysztof Karolak

Simple solution from here

这里简单的解决方案

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

You can specify version by changing setup_x.x value, for example to setup_5.x

您可以通过更改 setup_x.x 值来指定版本,例如更改为 setup_5.x