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

GitLab CI/CD构建失败:升级Node.js至18.17.1后无法找到node_modules

Node.js 18.17.1升级后GitLab CI/CD测试失败(无法定位aws-amplify模块)

错误详情

升级Node.js到18.17.1后,GitLab CI/CD流水线的测试环节报错,提示无法找到aws-amplify模块,具体错误日志如下:

FAIL src/services/authentication/index.spec.js
  ● Test suite failed to run
    Configuration error:
    Could not locate module aws-amplify mapped as:
    /builds/project-name/repository-name/node_modules/aws-amplify.
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/aws-amplify/": "/builds/project-name/repository-name/node_modules/aws-amplify"
      },
      "resolver": undefined
    }
      1 | /* eslint-disable camelcase */
    > 2 | import { Auth } from 'aws-amplify';
        | ^
      3 | import queryString from 'query-string';
      4 | import { localStorageService, httpService, sessionStorageService } from '..';
      5 | import environment from '../../config/environment';
    at createNoMappedModuleFoundError (../../../node_modules/jest-resolve/build/resolver.js:759:17)
    at Object.require (src/services/authentication/index.js:2:1)
    at Object.require (src/services/index.js:4:1)
    at Object.require (src/services/authentication/index.spec.js:1:1)
PASS src/redux/actions/session/action.spec.js
FAIL src/components/Home/index.spec.jsx

当前流水线脚本

dev-build:
  image: public.ecr.aws/8734dndn/node:18.17.1
  stage: dev-build
  script:
    - sh prepareenv.sh dev
    - cp environment.common.js ./src/config/environment/environment.dev.js
    - npm run test
    - npm run build development
  artifacts:
    paths:
      - dist
    expire_in: 1 week
  only:
    - master
    - /^RELEASE.*$/

解决方案

1. 修复Jest的moduleNameMapper配置

错误里的moduleNameMapper硬编码了绝对路径,CI环境中路径可能变化导致找不到模块。修改Jest配置文件(如jest.config.js):

module.exports = {
  // 保留其他配置,修改或移除以下行
  moduleNameMapper: {
    // 替换成相对路径,用<rootDir>指代项目根目录
    'aws-amplify': '<rootDir>/node_modules/aws-amplify',
    // 或者直接删除这个映射,让Jest自动解析模块
  }
};

2. 补充依赖安装步骤

当前流水线脚本完全没执行依赖安装!node_modules不存在肯定找不到模块,在script开头添加:

dev-build:
  # 其他配置不变
  script:
    - npm ci  # 优先用npm ci,基于lock文件安装更稳定
    - sh prepareenv.sh dev
    - cp environment.common.js ./src/config/environment/environment.dev.js
    - npm run test
    - npm run build development

如果没有package-lock.json,可以用npm install,但npm ci更适合CI场景。

3. 检查依赖与Node.js版本兼容性

确认aws-amplify版本是否支持Node.js 18.17.1:

  • 查看项目package.json中aws-amplify的版本,去其官方文档核对兼容的Node.js版本
  • 若版本不兼容,执行npm update aws-amplify升级到适配Node.js 18的版本

4. 清理CI缓存

CI缓存可能残留旧依赖或路径信息,导致冲突:

  • 可以在GitLab项目的「CI/CD」→「缓存」中手动清理缓存
  • 或者在流水线脚本中添加清理步骤:
script:
  - rm -rf node_modules package-lock.json
  - npm ci
  # 后续步骤...

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 20:40:36