ADO Pipeline发布npm包至本地Artifacts失败问题排查
核心问题分析
1. 发布包缺少构建代码
你的package.json中设置了"private": true,这会让npm默认跳过发布包的实际内容(仅保留package.json),导致其他项目安装时无法获取dist目录下的构建产物。另外,错误的npm publish工作目录配置也会导致发布内容异常。
2. ENOENT: no such file or directory, stat '/dist/*'错误
你在npm publish步骤中指定了workingDir: "/dist/",这是绝对路径,而Azure DevOps的Ubuntu代理机中,代码仓库是克隆到$(Build.SourcesDirectory)(比如/home/vsts/work/1/s)下的,根目录的/dist根本不存在,自然会报错。
分步解决方案
步骤1:修正package.json配置
打开项目根目录的package.json,做以下修改:
- 将
"private": true改为"private": false(私有包无法发布到内部/公共仓库,必须关闭私有标记) - 确认
files字段保留"dist",确保npm发布时包含构建产物:
"files": [ "dist" ]
步骤2:调整Pipeline YAML配置
2.1 修复npm publish工作目录
删除npm publish步骤中的workingDir: "/dist/"配置,让npm在项目根目录(包含package.json的位置)执行发布:
- task: Npm@1 displayName: npm publish inputs: command: publish publishRegistry: useFeed publishFeed: removed # 替换为你的实际Feed名称 verbose: true
2.2 调整版本更新与Git提交顺序
当前你先提交Git再执行npm version patch,这会导致版本号未同步到Git仓库。正确顺序是先更新版本(npm会自动修改package.json并创建commit),再推送Git:
# 移除原来的bash版本获取脚本,npm version会自动处理 - task: Npm@1 displayName: npm version patch inputs: command: "custom" customCommand: "version patch -force" - task: CmdLine@2 inputs: script: | git config --global user.email "ADOPipeline@company.com" git config --global user.name "Azure Pipeline" git push -u origin HEAD:master
2.3 移除不必要的步骤
注释掉的CopyFiles和PublishPipelineArtifact步骤不需要,因为npm publish会直接处理包的发布流程,无需额外复制或发布流水线工件。
步骤3:验证构建产物位置
从你的build日志可以看到,dist目录已经生成在项目根目录下(输出dist/base-theme.js等文件),无需额外移动文件,确保npm run build步骤在项目根目录执行即可(当前配置已满足)。
修改后的完整Pipeline关键片段
stages: - stage: Publish displayName: Publish Theme Package jobs: - job: Publish displayName: Publish to ADO Artifact feed pool: vmImage: "ubuntu-latest" steps: - checkout: self persistCredentials: true clean: true - task: npmAuthenticate@0 inputs: workingFile: .npmrc - task: NodeTool@0 displayName: "update node.js to 20.x" inputs: versionSource: "spec" versionSpec: "20.x" checkLatest: true - task: Npm@1 displayName: "Update npm to latest" inputs: command: "custom" customCommand: "install -g npm@latest" - task: Npm@1 displayName: npm install inputs: command: install - task: Npm@1 displayName: npm run build inputs: command: "custom" customCommand: "run build" - task: Npm@1 displayName: npm version patch inputs: command: "custom" customCommand: "version patch -force" - task: CmdLine@2 inputs: script: | git config --global user.email "ADOPipeline@company.com" git config --global user.name "Azure Pipeline" git push -u origin HEAD:master - task: Npm@1 displayName: npm publish inputs: command: publish publishRegistry: useFeed publishFeed: removed # 替换为你的Feed名称 verbose: true
验证结果
- 运行Pipeline,确认
npm publish步骤不再报错 - 在ADO Artifacts仓库中查看发布的包,确认包含dist目录下的所有构建文件
- 在其他项目中安装该包,验证是否能正常导入主题代码
内容的提问来源于stack exchange,提问作者Rob M

