centos 使用 nvm 安装 nodejs (nvm安装时出错,git 版本太老)

本文介绍在CentOS系统中安装NVM及Node.js的过程,包括解决因Git版本过旧导致的问题,并成功安装指定版本的Node.js。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装 nvm

使用以下命令安装nvm ,结果出错

wget -qO- https://raw.githubusercontent.com/cnpm/nvm/master/install.sh | bash

如下:

➜  ~ wget -qO- https://raw.githubusercontent.com/cnpm/nvm/master/install.sh | bash
=> Downloading nvm from git to '/root/.nvm'
=> Initialized empty Git repository in /root/.nvm/.git/
remote: Counting objects: 3928, done.
remote: Total 3928 (delta 0), reused 0 (delta 0), pack-reused 3928
Receiving objects: 100% (3928/3928), 967.69 KiB | 210 KiB/s, done.
Resolving deltas: 100% (2299/2299), done.
error: pathspec 'v0.26.1' did not match any file(s) known to git.

他说错误,也看不出来什么错误,换了一种方式,使用下面的命令安装

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

这时也报错了,如下:

➜  ~ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7731  100  7731    0     0   4345      0  0:00:01  0:00:01 --:--:--  5888
=> nvm is already installed in /root/.nvm, trying to update using git
=> error: pathspec 'v0.29.0' did not match any file(s) known to git.
Your version of git is out of date. Please update it!

=> Appending source string to /root/.zshrc
=> Close and reopen your terminal to start using nvm

不过他说 nvm 已经安装了 ,但是要更新 gitYour version of git is out of date. Please update it!

于是,看了以下 centos 系统里的 git 版本

➜  ~ git --version
git version 1.7.1

确实太老了,现在都 2.10 了,于是在 centos 6.7 上安装 git 的新版本了。

centos 系统中安装最新版 git ,请看我的另外一篇博文 《centos 6.7 安装 最新版 git》

nvm 安装完成后,可能要重启一下 shell 才有 nvm 这个命令。

重启 shell ,输入 nvm --version 可以查看当前 nvm 的版本,如下:

图片描述

安装 nodejs

查看都有哪些版本可以安装:

nvm ls-remote

可以看到当前最新版本是 v6.7.0 ,运行下面的命令来安装:

nvm install v4.6.0 #我安装的是 v4.6.0

查看安装的版本

node --version
 ➜  ~ nvm install v4.6.0
######################################################################## 100.0%
WARNING: checksums are currently disabled for node.js v4.0 and later
Now using node v4.6.0 (npm v2.15.9)
➜  ~ node --version
v4.6.0
➜  ~ 

查看一下当前已经安装的版本

nvm ls

切换版本

nvm use v4.6.0 

设置默认版本

nvm alias default v4.6.0

nvm 使用很简单 ,详细内容请使用 nvm help 查看帮助。

图片描述

<think>好的,用户想要在CentOS 7.9上运行Node.js项目,我得一步步来指导他们。首先,可能需要安装Node.js,CentOS默认的软件库可能版本比较旧,所以得考虑用其他方法,比如NodeSource的仓库。然后,安装npm或者yarn来管理依赖。接着,可能需要安装PM2这样的进程管理工具,保持应用持续运行。用户可能还需要配置防火墙,开放相关端口,比如3000或者用户项目用的端口。另外,可能需要设置反向代理,比如用Nginx,这样可以通过域名访问,或者处理SSL。还要确保项目的依赖都正确安装,比如运行npm install。如果项目有数据库,可能还需要安装和配置数据库服务,比如MongoDB或者MySQL。最后,可能需要指导如何启动应用,检查日志,处理常见错误。需要注意CentOS 7.9的兼容性,确认Node.js版本是否支持,避免使用新的版本导致问题。用户可能不熟悉Linux命令,需要给出具体的命令示例,并解释每个步骤的作用。另外,权限问题也是常见的,比如全局安装可能需要sudo,或者项目目录的权限设置。总之,要详细但清晰地引导用户完成整个流程,确保他们能顺利运行项目。</think>以下是在CentOS 7.9上运行Node.js项目的完整步骤指南: --- ### 一、环境准备 1. **更新系统** ```bash sudo yum update -y ``` 2. **安装开发工具链** ```bash sudo yum groupinstall "Development Tools" -y ``` --- ### 二、安装Node.js 推荐使用NodeSource仓库安装最新LTS版本: 1. **添加NodeSource仓库** ```bash curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash - ``` 2. **安装Node.js** ```bash sudo yum install -y nodejs ``` 3. **验证安装** ```bash node -v # 应显示v18.x或更高版本 npm -v ``` --- ### 三、部署Node.js项目 1. **上传项目文件** 使用SFTP/scp或git克隆项目到服务器: ```bash git clone <your_project_repo> cd your-project ``` 2. **安装依赖** ```bash npm install --production # 生产环境建议使用此参数 ``` 3. **配置环境变量** 创建.env文件: ```bash NODE_ENV=production PORT=3000 DATABASE_URL=your_db_connection_string ``` --- ### 四、进程管理(推荐PM2) 1. **全局安装PM2** ```bash sudo npm install pm2@latest -g ``` 2. **启动应用** ```bash pm2 start app.js --name "my-app" ``` 3. **设置开机启动** ```bash pm2 startup pm2 save ``` --- ### 五、网络配置 1. **开放防火墙端口** ```bash sudo firewall-cmd --permanent --add-port=3000/tcp sudo firewall-cmd --reload ``` 2. **配置Nginx反向代理(可选)** ```nginx server { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } ``` 重启Nginx: ```bash sudo systemctl restart nginx ``` --- ### 六、维护命令 | 操作 | 命令 | |------|------| | 查看实日志 | `pm2 logs` | | 重启应用 | `pm2 restart my-app` | | 停止应用 | `pm2 stop my-app` | | 查看进程列表 | `pm2 list` | --- ### 常见问题排查 1. **端口冲突**: ```bash netstat -tuln | grep <port> ``` 2. **权限问题**: ```bash sudo chown -R $(whoami):$(whoami) /path/to/project ``` 3. **内存不足**: 使用Node.js内存限制参数: ```bash pm2 start app.js --max-memory-restart 500M ``` --- 建议使用最新LTS版本Node.js以获得更好的安全性和性能支持。如果是旧项目,可通过`nvm`管理多版本Node环境: ```bash curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash nvm install 16 # 示例:安装Node.js 16 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值