通常情况下,不需要将 node_modules 目录提交到 GitLab 中。GitLab CI / CD 可以使用 Yarn 或 npm 安装这些依赖项。以下是示例 .gitlab-ci.yml 文件:
image: node:latest
stages:
- build
- test
cache:
paths:
- node_modules/
before_script:
- npm install
build:
stage: build
script:
- npm run build
test:
stage: test
script:
- npm run test
此 .gitlab-ci.yml 文件使用 Node.js 官方镜像来运行构建,并确保 node_modules 缓存可用。before_script 部分为每个作业安装了项目的依赖项。build 和 test 阶段执行项目构建和测试命令。通过使用 npm install 命令,CI 系统会在构建作业之前自动安装依赖项。
通过这种方法,将 node_modules 目录提交到版本控制中是不必要的,可以加快代码提交速度并减少存储库大小。