深色模式
NodeJS 学习笔记
Node.js 官网 —— https://nodejs.org/
Node.js 中文网 —— http://nodejs.cn/
Cnode - Node.js 专业中文社区 —— https://cnodejs.org/
什么是 NodeJS
Node.js® 是一个免费、开源、跨平台的 JavaScript 运行时环境, 它让开发人员能够创建服务器 Web 应用、命令行工具和脚本。
Microsoft Learn:可使用 NodeJS 执行什么操作?
入门推荐
Microsoft Learn:Microsoft NodeJS 入门及安装教程
Microsoft Learn:使用 Node.js 生成 JavaScript 应用程序学习路径
安装 Node.js
在 Windows 上安装 Node.js 开发环境
Node.js 官网下载安装:https://nodejs.org/zh-cn/download
Microsoft Learn:在 Windows 上安装 Node.js
在 WSL 上安装 Node.js 开发环境
Microsoft Learn:在适用于 Linux 的 Windows 子系统 (WSL2) 上安装 Node.js
通过 nvm 安装 Node.js
sh
sudo apt-get install curl # 安装 cURL
1
sh
# 安装 NVM (Node 版本管理器)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
1
2
2
如提示错误: Failed to connect to raw.githubusercontent.com
或 可尝试更改 DNS 为 8.8.8.8
或 可尝试打开 https://www.ipaddress.com/ 查询 raw.githubusercontent.com 对应的 IP 地址,替换系统的 hosts 文件
或 可尝试使用国内源
sh
# 国内源
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash -- --mirror=https://npm.taobao.org/mirrors/nvm
1
2
2
sh
# 直接可调用 nvm 和加载自动补全
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
1
2
3
4
2
3
4
sh
source ~/.bashrc # 这样也可
1
sh
# 列出当前安装的 Node.js 版本
nvm ls
1
2
2
sh
# 安装 Node.js 的当前稳定的 LTS 版本(推荐用于生产应用程序)
nvm install --lts
1
2
2
sh
# 安装 Node.js 的当前版本(用于测试最新的 Node.js 功能和改进,但更容易出现问题)
nvm install node
1
2
2
sh
node --version # 查看 Node.js 版本
npm --version # 查看 npm 版本
npx --version # 查看 npx 版本
1
2
3
2
3
通过 n 安装 Node.js
可以安装 n 来管理和变更 node 版本。 https://www.npmjs.com/package/n
sh
sudo apt install npm
npm install -g n
1
2
2
npm 可更改国内源
sh
# 可设置为国内镜像源
npm config set registry https://registry.npm.taobao.org/ # 淘宝镜像源
npm config set registry http://mirrors.cloud.tencent.com/npm/ # 腾讯云镜像源
npm config set registry https://mirrors.huaweicloud.com/repository/npm/ # 华为云镜像源
# nrm(NPM Registry Manager)工具,它可以帮助您管理不同的 npm 源
npm install -g nrm
nrm ls # 查看所有可用的源
nrm use taobao # 使用淘宝镜像源
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
包管理工具
sh
node --version # 查看 Node.js 版本
npm --version # 查看 npm 版本
npx --version # 查看 npx 版本
pnpm --version # 查看 pnpm 版本
yarn --version # 查看 yarn 版本
bun --version # 查看 bun 版本
1
2
3
4
5
6
2
3
4
5
6
安装 pnpm
sh
corepack enable pnpm # 使用 Corepack 安装
1
sh
npm install -g pnpm # 使用 npm 安装
1
安装 yarn
https://yarnpkg.com/getting-started/install
管理 Yarn 的首选方法是按项目并通过 Corepack,这是 Node. js 默认提供的工具。
sh
corepack enable # 启用 Corepack
1
sh
yarn init -2 # 初始化 Yarn
1
如果执行 yarn 出现问题
00h00m00s 0/0: : ERROR: There are no scenarios; must have at least one.
1
可尝试:
sh
sudo apt remove cmdtest
sudo apt remove yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn
1
2
3
4
5
6
2
3
4
5
6
安装 Bun
sh
sudo apt-get install unzip
curl -fsSL https://bun.sh.cn/install | bash # for macOS, Linux, and WSL
1
2
2
sh
npm install -g bun # 使用 npm 安装
1