Jenkins构建React项目时node_modules未排除导致体积过大怎么办?

百里凌薇 阅读 71

我在Jenkins构建React项目时发现生成的dist文件有几百MB,怀疑是node_modules被错误打包进去了。按照网上的方法在Jenkinsfile里加了排除项,但没效果…

我的Jenkinsfile配置片段是这样的:


pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }
        stage('Archive') {
            steps {
                archiveArtifacts artifacts: 'dist/**', excludes: 'node_modules/**'
            }
        }
    }
}

项目结构正常,比如App组件:


import React from 'react';
import './App.css';

function App() {
  return ;
}

export default App;

尝试过在npm install后加rm -rf node_modules,但构建失败说找不到模块。用docker agent时问题更严重,dist里居然真的包含了完整node_modules目录,怎么办?

我来解答 赞 8 收藏
二维码
手机扫码查看
2 条解答
打工人珮青
,先搞清楚你遇到的问题根源。你说 dist 里包含了 node_modules,这个通常不是 Webpack 默认行为。React 项目构建时一般只会打包 src 目录下的文件,但如果你用了某些自定义 Webpack 配置,或者用了 copy-webpack-plugin 这类插件,就有可能误把 node_modules 拷进去。

,先检查你项目下的 webpack.config.js 或 build 工具配置。create-react-app 默认不会打包 node_modules,如果你用了 custom Webpack config,看看 entry 和 output 设置对不对,特别是有没有错误地把整个项目目录作为资源复制进去。

,解决 Jenkins 构建产物的问题。你现在用的 archiveArtifacts 插件是正确的,但它只负责归档,不会影响构建本身。你看到的 node_modules 出现在 dist 里,大概率是你的构建过程出了问题。可以先在本地运行 npm run build,检查 dist 目录内容,如果本地没问题,再看 Jenkins 的构建流程。

,调整 Jenkinsfile。你现在写的 excludes: 'node_modules/' 是归档时排除,不影响 dist 生成。要真正解决 dist 体积问题,应该聚焦在构建阶段。你提到用 docker agent 时更严重,那可能是你用了不合适的 base image 或者 COPY 方式错误。比如在 Dockerfile 里用了 COPY . /app,就会把 node_modules 拷进去。

建议你修改 Jenkinsfile,先排除 node_modules 不参与构建,再归档 dist,下面是修改后的 Jenkinsfile 示例:

pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
}
}
}

另外,确保你没有在构建脚本里手动复制 node_modules 到 dist。如果你用的是 copy-webpack-plugin,检查它的配置,不要把 node_modules 包含进去。

最后,在本地或 Jenkins 上运行构建前,先清理环境,比如:

sh 'rm -rf dist/'
sh 'npm run build'

这样确保每次构建都是干净的,避免旧文件残留导致体积膨胀。
点赞 5
2026-02-06 14:26
UX-利伟
UX-利伟 Lv1
你的问题确实是 node_modules 被打包进去导致的,不过 archiveArtifactsexcludes 参数只会在归档阶段起作用,而不会阻止 npm run build 把不该打包的东西打进去了。更好的写法是从构建过程本身入手,确保生成的 dist 里干净无污染。

以下是解决方案:

### 1. 确保 Webpack 配置正确
如果你用的是 Create React App 或类似的脚手架,默认情况下 node_modules 不会被打包进去。但如果自定义了 Webpack 配置,可能需要检查 externals 或者其他相关配置。如果不确定,可以先尝试清理缓存重新构建:
rm -rf node_modules dist && npm cache clean --force && npm install && npm run build


### 2. 使用 .gitignore.dockerignore
如果是 Docker 构建环境,建议在 Dockerfile 或 .dockerignore 中明确排除 node_modules,避免它被复制到镜像中。例如:
# .dockerignore
node_modules/


### 3. 修改 Jenkinsfile
在构建阶段,明确清理掉 node_modules 的残留。以下是一个更优雅的 Jenkinsfile 写法:
pipeline {
agent any
stages {
stage('Build') {
steps {
// 安装依赖
sh 'npm ci' // 比 npm install 更快更稳定
// 构建项目
sh 'npm run build'
// 清理 node_modules,防止误打包
sh 'rm -rf node_modules'
}
}
stage('Archive') {
steps {
// 归档,确保只有 dist 目录的内容被打包
archiveArtifacts artifacts: 'dist/**', fingerprint: true
}
}
}
}


注意:这里用了 npm ci,它是生产环境更推荐的方式,能锁定版本,避免因为 package-lock.json 和实际依赖不一致导致的问题。

### 4. 验证生成的 dist
最后,在本地测试一下 npm run build 是否真的包含多余的 node_modules 文件夹。如果还有问题,可能是你的构建工具配置有问题,需要进一步排查。

总之,关键是把构建和归档的职责分开,确保 node_modules 不会出现在最终的 dist 里。希望这些改动能帮你解决大文件问题!
点赞 9
2026-01-30 13:03