在 linux 上安装 nodejs 和 npm [英] Installing nodejs and npm on linux

查看:51
本文介绍了在 linux 上安装 nodejs 和 npm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 linux 服务器(这是一个运行 raspbian 的 pi)上安装 nodejs 和 npm 时遇到了一些问题.我使用

I have having a bit of an issue with installing nodejs and npm on my linux server (which is a pi running raspbian). I had everything set up and running using

sudo apt-get install nodejs npm

一切都很好,很花哨,直到我发现这些版本显然已经过时了.所以我删除了它们

All was fine and dandy, until I found out that apparently these versions are now old. So I removed them

sudo apt-get purge nodejs npm

然后我找到了以下答案(here) 在 SO 上运行

Then I found the following answer (here) on SO and ran

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

运行 node -v 有我的版本 0.6.19...我假设它转换为版本 6.19 而不是版本 0.但是,运行 npm -v 告诉我它没有安装.所以我再次清除了nodejs,并寻找了另一种解决方案.那时我决定关注 nodejs 网站上的内容(这里).我运行了以下命令.

Running node -v have me version 0.6.19...which I'm assuming translates to version 6.19 as opposed to version 0. However, running npm -v told me that it was not installed. So I once again purged nodejs, and looked for another solution. At which point I decided to follow the stuff on nodejs's site (here). And I ran the following commands.

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

sudo apt-get install -y build-essential

2 个问题:

1) 安装的版本仍然是 0.6.19.我宁愿使用 4.x 版,因为那是我在我的开发机器(macOS Sierra)上运行的版本.

1) The version installed was still 0.6.19. I would rather have version 4.x, since that's what I'm running on my dev machine (macOS Sierra).

2) 我仍然没有 npm.这使得 nodejs 无用

2) I still don't have npm. Which renders nodejs useless

对任何一个(但最好是 2 个)的任何帮助都会很棒.提前致谢.

Any help on either (but preferably 2) would be great. Thanks in advance.

推荐答案

我真的建议你使用 nvm 安装 node 和 npm.这是最快、最干净、最简单的方法.

I really recommend you install node and npm using nvm. This is the fastest, cleanest and easiest way to do it.

这样,您只需执行以下操作即可安装 NVM:

That way, you install NVM simply doing:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash

要测试 nvm 是否已正确安装,请关闭并重新打开终端并输入 nvm.如果您收到 nvm: command not found 消息,则您的操作系统可能没有必要的 .bash_profile 文件.在终端中,输入 touch ~/.bash_profile 并再次运行上述安装脚本.

To test that nvm was properly installed, close and re-open Terminal and enter nvm. If you get a nvm: command not found message, your OS may not have the necessary .bash_profile file. In Terminal, enter touch ~/.bash_profile and run the above install script again.

您现在可以安装节点输入:

And you are now able to install node typing:

nvm install <version>

例如

nvm install 4.2.1

如果你只是想安装最新的节点版本,你可以直接输入

if you just want to install the latest node version, you can just type

nvm install node

为了以 sudo 身份访问 node 和 npm(为了拥有 <1024 个端口),您应该运行

In order to access node and npm as sudo (in order to have <1024 ports) you should run

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

这篇关于在 linux 上安装 nodejs 和 npm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆