三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

WSL Ubuntu中安装Mermaid CLI失败解决

WSL Ubuntu中安装Mermaid CLI失败解决

在WSL中安装Mermaid CLI失败时,可参考以下详细解决方案:

1. Node.js环境问题

  • 前置条件:确保已安装Node.js(建议v16+)
  • 检查版本:运行node --version验证
  • 安装失败时:使用以下命令安装Node.js:
# 使用nvm安装Node.js(推荐)curl-o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh|bashsource~/.bashrc nvminstall--ltsnvm use--lts
  • 清理npm缓存:npm cache clean --force
  • 永久设置镜像:npm config set registry https://registry.npmmirror.com

2. Puppeteer依赖问题

  • 核心原因:Mermaid CLI依赖Puppeteer,而Puppeteer默认会下载Chromium,可能因权限或路径问题失败
  • 解决方案
  • 跳过Chromium下载:设置环境变量export PUPPETEER_SKIP_DOWNLOAD=true
  • Ubuntu系统直接安装Chromium(推荐):
# 安装Chromium浏览器sudoapt-getupdatesudoapt-getinstall-ychromium-browser# 配置Puppeteer使用系统ChromiumexportPUPPETEER_EXECUTABLE_PATH=$(whichchromium-browser)# 永久生效(添加到~/.bashrc)echo'export PUPPETEER_EXECUTABLE_PATH=$(which chromium-browser)'>>~/.bashrcsource~/.bashrc
  • 备选方案:手动安装Chrome
# 下载并安装Chromewgethttps://dl.google.com/linux/direct/google-chrome-stable_current_amd64.debsudoaptinstall./google-chrome-stable_current_amd64.debrmgoogle-chrome-stable_current_amd64.deb# 设置环境变量exportPUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chromeecho'export PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome'>>~/.bashrcsource~/.bashrc

3. 网络问题

  • 国内用户推荐:安装时强制使用国内镜像
npminstall-g@mermaid-js/mermaid-cli--verbose--registry=https://registry.npmmirror.com
  • 证书过期:若遇CERT_HAS_EXPIRED错误,切换镜像源
npmconfigsetregistry https://registry.npmmirror.com

4. 权限问题

  • 提升权限安装:在安装命令前添加sudo,避免全局安装路径无写入权限
  • 检查权限:确保用户对~/.cache/puppeteer有读写权限

5. 其他问题

  • 检查日志:安装失败时查看详细日志定位问题:cat ~/.npm/_logs/*.log
  • 使用Docker:若环境配置复杂,可用Docker容器替代
← 返回列表