跳到主要内容

在WSL上安装Node.js

· 阅读需 2 分钟

如何在wsl上安装node.js 环境

配置本地代理

由于无法顺利访问Github,所以要配置 wsl 的本地代理: 本质上配置一个环境编辑就可以了:

export ALL_PROXY="http://172.19.80.1:7890"

配置步骤,先在代理软件上开启本地网络代理:

Proxy switch

使用脚本一键配置,脚本内容如下:

#!/bin/bash
host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")
export ALL_PROXY="http://$host_ip:7890"

安装Node js 的管理器 nvm

nvm 是 Node Version Manager 的缩写,它是一个用于管理 Node.js 版本的命令行工具。通过 nvm,你可以在同一台机器上安装和切换多个 Node.js 版本,这对于测试在不同 Node.js 版本上的应用程序兼容性非常有用。

在 Linux 上安装 nvm,你可以使用 cURL 或 Wget 运行安装脚本。以下是步骤:

打开你的终端。 使用 cURL 下载安装脚本:

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

或者使用 Wget:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

该脚本会将 nvm 仓库克隆到 ~/.nvm,并将源行添加到你的配置文件(~/.bash_profile,~/.zshrc,~/.profile 或 ~/.bashrc)。

关闭你的终端并打开一个新的终端。

通过检查 nvm 的版本来验证安装是否成功:

nvm --version

记得将 v0.38.0 替换为最新可用的版本。

安装NodeJS

使用nvm命令安装NodeJS

nvm install 18

切换Node js版本

nvm use 18

检查已经安装的Node js

nvm list

检查node js 版本

node -v