一、前置基础——02-开发环境搭建/01-Node.js安装与版本管理

发布时间:2026/5/26 0:45:20

一、前置基础——02-开发环境搭建/01-Node.js安装与版本管理 02-开发环境搭建/01-Node.js安装与版本管理Node.js 安装与版本管理学习目标掌握在不同操作系统上安装 Node.js 的方法学会使用 nvm 管理多个 Node.js 版本理解 Node.js 版本策略LTS vs Current配置 npm 全局模块路径前置知识基本的命令行操作文件系统基础概念知识点列表1. Node.js 版本介绍1.1 版本号规则语义化版本major.minor.patchmajor不兼容的 API 变更minor向后兼容的功能新增patch向后兼容的问题修复1.2 LTS vs CurrentLTSLong Term Support长期支持版本适合生产环境维护周期30个月偶数版本号如14.x、16.x、18.xCurrent当前最新版本适合开发测试包含最新特性奇数版本号如15.x、17.x、19.x# 查看当前 Node.js 版本node--version# 或node-v# 查看 npm 版本npm--versionnpm-v2. 各平台安装方法2.1 Windows 安装2.1.1 官方安装包推荐新手访问 https://nodejs.org下载 LTS 版本 .msi 安装包运行安装程序一路 Next验证安装node--versionnpm--version2.1.2 使用 Chocolatey包管理器# 安装 LTS 版本chocoinstallnodejs-lts# 安装 Current 版本chocoinstallnodejs2.2 macOS 安装2.2.1 官方安装包推荐新手访问 https://nodejs.org下载 .pkg 安装包运行安装程序2.2.2 使用 Homebrew推荐开发者# 安装 Homebrew如未安装/bin/bash-c$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)# 安装 Node.jsbrewinstallnode# 安装 LTS 版本brewinstallnode18# 切换版本brewlink--overwritenode182.3 Linux 安装2.3.1 Ubuntu/Debian# 使用 NodeSource 官方源curl-fsSLhttps://deb.nodesource.com/setup_18.x|sudo-Ebash-sudoapt-getinstall-ynodejs# 安装开发工具编译原生模块sudoapt-getinstall-ybuild-essential2.3.2 CentOS/RHEL# 使用 NodeSource 官方源curl-fsSLhttps://rpm.nodesource.com/setup_18.x|sudobash-sudoyuminstall-ynodejs# 安装开发工具sudoyuminstall-ygcc-cmake3. 版本管理工具推荐3.1 nvmNode Version Manager- 最流行3.1.1 安装 nvm# macOS/Linuxcurl-o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh|bash# 或者使用 wgetwget-qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh|bash# 重启终端或执行source~/.bashrc# 或 source ~/.zshrc# Windows 使用 nvm-windows# 下载地址https://github.com/coreybutler/nvm-windows/releases3.1.2 nvm 常用命令# 查看已安装版本nvm list# 查看可远程安装的版本nvm list available nvm ls-remote# 安装指定版本nvminstall18.17.0# 安装特定版本nvminstall18# 安装最新 18.xnvminstallnode# 安装最新 Current# 切换版本nvm use18.17.0# 当前会话切换nvmaliasdefault18.17.0# 设置默认版本# 卸载版本nvm uninstall16.20.0# 查看当前版本nvm current# 执行命令使用指定版本nvmexec14.21.0nodeapp.js3.2 n简单易用# 安装 n需要先有 Node.jsnpminstall-gn# 安装 LTS 版本n lts# 安装最新版本n latest# 安装指定版本n18.17.0# 切换版本n# 删除版本nrm16.20.03.3 fnm快速版本管理器 - Rust 编写# macOS/Linux 安装curl-fsSLhttps://fnm.vercel.app/install|bash# 使用fnm list-remote# 查看可用版本fnminstall18.17.0# 安装版本fnm use18.17.0# 使用版本fnm default18.17.0# 设置默认4. npm 配置优化4.1 配置全局安装路径# 查看当前配置npmconfig list# 设置全局模块安装路径npmconfigsetprefix~/.npm-global# 添加到 PATH在 ~/.bashrc 或 ~/.zshrcexportPATH~/.npm-global/bin:$PATH# 重新加载配置source~/.bashrc4.2 配置镜像源# 查看当前源npmconfig get registry# 切换到淘宝镜像npmconfigsetregistry https://registry.npmmirror.com# 切换回官方源npmconfigsetregistry https://registry.npmjs.org# 使用 cnpmnpminstall-gcnpm--registryhttps://registry.npmmirror.com# 使用 nrm 管理源npminstall-gnrm nrmls# 列出所有源nrm use taobao# 切换源nrmtest# 测试速度4.3 配置代理# 设置代理npmconfigsetproxy http://proxy.company.com:8080npmconfigsethttps-proxy http://proxy.company.com:8080# 取消代理npmconfig delete proxynpmconfig delete https-proxy# 设置 strict-ssl内部证书问题npmconfigsetstrict-sslfalse5. 验证安装5.1 基础验证# 检查 Node.js 版本node-v# 检查 npm 版本npm-v# 检查 npx 版本npx-v# 查看 Node.js 路径whichnode# 查看 npm 全局路径npmroot-g5.2 运行测试脚本// test.jsconsole.log(Node.js 版本:,process.version);console.log(当前平台:,process.platform);console.log(当前目录:,process.cwd());consthttprequire(http);constserverhttp.createServer((req,res){res.writeHead(200);res.end(Hello Node.js!);});server.listen(3000,(){console.log(服务器运行在 http://localhost:3000);});# 运行测试nodetest.js# 浏览器访问 http://localhost:3000# CtrlC 停止服务器6. 常见问题解决6.1 权限问题# EACCES 权限错误# 方法1修复 npm 权限sudochown-R$(whoami)~/.npm# 方法2使用 nvm 避免权限问题# 方法3修改全局目录mkdir~/.npm-globalnpmconfigsetprefix~/.npm-global6.2 环境变量问题# macOS/Linux - 添加到 ~/.bashrc 或 ~/.zshrcexportPATH/usr/local/bin:$PATHexportNODE_PATH/usr/local/lib/node_modules# Windows - 系统属性 环境变量# 添加 NODE_PATHC:\Users\用户名\AppData\Roaming\npm\node_modules6.3 版本冲突# 清理 npm 缓存npmcache clean--force# 删除 node_modules 重装rm-rfnode_modulesnpminstall# 使用 nvm 切换版本nvm use18代码示例示例1版本检查脚本// version-check.jsconstosrequire(os);console.log( Node.js 环境信息 );console.log(Node.js 版本:${process.version});console.log(npm 版本:${require(child_process).execSync(npm -v).toString().trim()});console.log(操作系统:${os.platform()}${os.arch()});console.log(当前用户:${os.userInfo().username});console.log(CPU 核心数:${os.cpus().length});console.log(总内存:${(os.totalmem()/1024/1024/1024).toFixed(2)}GB);console.log();// 检查必要工具consttools[node,npm,npx];tools.forEach(tool{try{constversionrequire(child_process).execSync(${tool}-v).toString().trim();console.log(${tool}:${version});}catch(e){console.log(${tool}: 未安装);}});示例2环境配置脚本// setup-env.jsconstfsrequire(fs);constpathrequire(path);constosrequire(os);functioncheckNodeEnvironment(){constresults{node:process.version,npm:null,platform:os.platform(),arch:os.arch(),envPaths:{nodePath:process.env.NODE_PATH||未设置,npmPrefix:null,npmCache:null}};try{results.npmrequire(child_process).execSync(npm -v).toString().trim();results.envPaths.npmPrefixrequire(child_process).execSync(npm config get prefix).toString().trim();results.envPaths.npmCacherequire(child_process).execSync(npm config get cache).toString().trim();}catch(e){results.npm无法获取;}returnresults;}functiongenerateEnvFile(){constenvContent# Node.js 环境配置 NODE_VERSION${process.version}NPM_VERSION${require(child_process).execSync(npm -v).toString().trim()}NODE_ENVdevelopment # 路径配置 NODE_PATH${path.join(os.homedir(),.npm-global,lib,node_modules)}# 镜像源可选 # npm config set registry https://registry.npmmirror.com.trim();constenvPathpath.join(process.cwd(),.env.example);fs.writeFileSync(envPath,envContent);console.log(已生成环境配置文件:${envPath});}// 执行console.log(checkNodeEnvironment());generateEnvFile();练习题基础题使用 nvm 安装 Node.js 18.x 和 20.x 两个版本并切换使用配置 npm 使用淘宝镜像源安装一个全局包测试编写一个脚本输出当前 Node.js 的安装路径和全局模块路径进阶题创建一个 shell 脚本自动检测并安装合适的 Node.js 版本配置多个项目使用不同的 Node.js 版本实现自动切换练习题参考答案基础题1 - 使用 nvm# 安装版本nvminstall18nvminstall20# 查看已安装nvm list# 切换版本nvm use18node-v# 验证nvm use20node-v# 验证# 设置默认nvmaliasdefault18基础题2 - 配置镜像# 配置淘宝镜像npmconfigsetregistry https://registry.npmmirror.com# 验证配置npmconfig get registry# 安装全局包测试npminstall-gnodemon# 恢复官方源npmconfigsetregistry https://registry.npmjs.org基础题3 - 路径检查脚本constpathrequire(path);const{execSync}require(child_process);console.log(Node.js 路径:,process.execPath);console.log(npm 全局模块路径:,execSync(npm root -g).toString().trim());console.log(npm 缓存路径:,execSync(npm config get cache).toString().trim());console.log(当前 PATH:,process.env.PATH);

相关新闻