使用Yarn Berry与GitHub Action构建CI时遇YAML语法异常排查
Yarn Berry + GitHub Action CI工作流YAMLException错误排查
在使用Yarn Berry搭配GitHub Action搭建CI工作流时,触发了如下YAMLException错误:
Error: YAMLException: end of the stream or a document separator is expected at line 3, column 11: nodeLinker: pnp ^ at generateError (/usr/local/lib/node_modules/yarn/lib/cli.js:126542:10) at throwError (/usr/local/lib/node_modules/yarn/lib/cli.js:126548:9) at readDocument (/usr/local/lib/node_modules/yarn/lib/cli.js:127914:5) at loadDocuments (/usr/local/lib/node_modules/yarn/lib/cli.js:127950:5) at load (/usr/local/lib/node_modules/yarn/lib/cli.js:127971:19) at safeLoad (/usr/local/lib/node_modules/yarn/lib/cli.js:127993:10) at parse (/usr/local/lib/node_modules/yarn/lib/cli.js:64242:18) at module.exports.exports.default (/usr/local/lib/node_modules/yarn/lib/cli.js:63793:96) at loadRcFile (/usr/local/lib/node_modules/yarn/lib/cli.js:57527:58) at /usr/local/lib/node_modules/yarn/lib/cli.js:57501:14
用户提供的.yarnrc.yml文件内容如下:
.yarnrc.yml nodeLinker: pnp yarnPath: .yarn/releases/yarn-4.4.1.cjs
同时附上GitHub Action工作流配置:
name: CI Workflow on: push: branches: [main, develop] pull_request: branches: [main, develop] jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [18.x, 20.x] steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Node.js ${{ matrix.node-version }} uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} cache: 'yarn' - name: Enable Corepack run: corepack enable - name: Install dependencies run: yarn install - name: Run tests run: yarn test --passWithNoTests - name: Build project run: yarn build
错误原因
问题出在.yarnrc.yml文件的第一行——你错误地将文件名.yarnrc.yml写入了文件内容中。YAML解析器会把这行当作配置内容的一部分,但它既不是合法的键值对,也不符合YAML的语法结构,直接导致了解析失败。
修复方案
- 修正
.yarnrc.yml文件:删除第一行的.yarnrc.yml文字,保留正确的配置项:
nodeLinker: pnp yarnPath: .yarn/releases/yarn-4.4.1.cjs
- 优化GitHub Action工作流:为了避免系统全局Yarn版本与项目配置版本冲突,在
Enable Corepack步骤后添加指定项目Yarn版本的步骤:
- name: Use project configured Yarn version run: yarn set version .yarn/releases/yarn-4.4.1.cjs
内容的提问来源于stack exchange,提问作者gmulb
相关产品推荐
相关产品推荐

