You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地构建Next.js项目失败,报错TypeError: n.replaceAll is not a function

Next.js构建报错:TypeError: n.replaceAll is not a function

问题详情

构建Next.js项目时触发静态页面生成错误,具体提示:

Generating static pages (0/6)TypeError: n.replaceAll is not a function

完整错误栈:

Error occurred prerendering page "/404". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: n.replaceAll is not a function
    at file:///home/sev/dev/frontend/node_modules/connectkit/build/index.es.js:682:37384
    at Array.forEach (<anonymous>)
    at rn (file:///home/sev/dev/frontend/node_modules/connectkit/build/index.es.js:682:37367)
    at file:///home/sev/dev/frontend/node_modules/connectkit/build/index.es.js:682:37304
    at Array.map (<anonymous>)
    at tn (file:///home/sev/dev/frontend/node_modules/connectkit/build/index.es.js:682:37270)
    at kn (file:///home/sev/dev/frontend/node_modules/connectkit/build/index.es.js:682:42706)
    at Wc (/home/sev/dev/frontend/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:68:44)
    at Zc (/home/sev/dev/frontend/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:70:253)
    at Z (/home/sev/dev/frontend/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:76:89)

已做排查操作:

  • 删除node_modules和.next文件夹后重新执行yarn install --frozen-lockfile + next build → 失败
  • 新文件夹克隆仓库后执行上述构建流程 → 失败
  • GitHub Action中运行构建 → 成功
  • 用nektos/act在本地模拟GitHub Action运行 → 成功
  • 同事电脑执行相同本地构建步骤 → 成功

问题原因

replaceAll是ES2021新增的字符串方法,仅在Node.js 15.0.0及以上版本中支持。你的本地Node.js版本低于该要求,而GitHub Action环境、同事电脑使用的Node.js版本符合支持标准,因此只有本地构建失败。

解决方法

  1. 检查本地Node.js版本:
    node -v
    
  2. 升级Node.js到15.0.0或更高版本(推荐使用LTS稳定版,如18.x、20.x):
    • 若使用nvm版本管理器:
      nvm install lts/hydrogen  # 安装Node.js 18.x LTS
      nvm use lts/hydrogen
      
    • 若使用n版本管理器:
      n install lts
      
    • 也可直接下载对应版本安装包手动升级
  3. 升级完成后,重新执行构建流程:
    yarn install --frozen-lockfile
    next build
    

验证升级成功:执行以下命令,若输出xexx则说明版本已支持replaceAll

node -e "console.log('test'.replaceAll('t', 'x'))"

内容的提问来源于stack exchange,提问作者sev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 11:35:13