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

Lerna发布的包含TypeScript源码而非编译后JavaScript文件的问题

问题:Lerna + GitHub Actions 发布的包包含TS源码而非编译后JS文件

使用Lerna结合GitHub Actions发布包时,发现安装后的包目录里是src下的TypeScript源码文件,而非预期的dist目录编译后的JavaScript文件,相关配置如下:

Lerna配置(lerna.json)

{
  "$schema": "node_modules/lerna/schemas/lerna-schema.json",
  "version": "independent",
  "npmClient": "yarn",
  "command": {
    "publish": {
      "registry": "https://npm.pkg.github.com/",
      "conventionalCommits": true
    },
    "version": {
      "conventionalCommits": true,
      "message": "chore(release): publish"
    }
  },
  "packages": ["src/modules/*"]
}

根package.json脚本配置

{
   "name": "@example/pack",
   "author": "example",
   "version": "1.0.0",
   "description": "",
   "main": "./dist/index.js",
   "scripts": {
     "build": "rm -rf dist && tsc",
     "patch:version": "yarn version --patch --no-git-tag-version",
     "copy:dependencies": "cp package.json dist/package.json && cd dist",
     "npm:publish": "npm run build &&npm run patch:version && npm run copy:dependencies && npm publish"
   },
   "dependencies": {
 ...
   },
   "devDependencies": {
  ...
   },
   "workspaces": [
     "src/modules/module1",
     "src/modules/module2",
     "src/modules/module3",
     "src/modules/module14"
   ],
   "keywords": [],
   "license": "ISC"
 }

GitHub Actions工作流

name: Automated Build and Deployment

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install Dependencies
        run: npm install

      - name: Build Packages
        run: npx lerna run build

      - name: Publish to GitHub Packages
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          npm run copy:dependencies
          npx lerna publish --no-git-tag-version --no-push --y

目录结构

+----------------------+
| (directory structure) |
+----------------------+
|    dist/
|    node_modules/
|    src/
|        helpers/
|        modules/
|        shared/
|    .gitignore
|    .npmignore
|    .prettierrc
|    lerna.json
|    package.json
|    tsconfig.json
|    yarn.lock
+----------------------+

解决方案

问题核心是Lerna默认会发布每个子包目录下的所有文件(除被忽略项),但当前配置未指定仅发布编译后的dist目录,也未正确排除源码文件。按以下步骤修复:

1. 给每个子包配置package.json

每个子包(src/modules/module1等)需单独设置:

  • 将main字段指向编译后的入口文件,比如./dist/index.js
  • 添加files字段,明确指定要发布的内容(仅保留dist目录和必要配置)
  • 确保子包的build脚本能将TS代码编译到自身的dist目录

子包package.json示例:

{
  "name": "@example/module1",
  "version": "1.0.0",
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts", // 需要发布类型声明时添加
  "files": [
    "dist/**/*",
    "package.json"
  ],
  "scripts": {
    "build": "rm -rf dist && tsc"
  }
}

2. 用.npmignore或files控制发布内容

  • 若使用files白名单,无需额外配置.npmignore(白名单优先级更高)
  • 若不用files,则在根目录和每个子包目录的.npmignore中添加:
# .npmignore
src/
node_modules/

3. 修正GitHub Actions发布步骤

当前的npm run copy:dependencies是针对根包的操作,但Lerna发布的是子包,该步骤完全多余,直接删除即可:

- name: Publish to GitHub Packages
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          npx lerna publish --no-git-tag-version --no-push --yes

4. 确认Lerna触发所有子包构建

保证每个子包都有build脚本,执行npx lerna run build会遍历所有子包,触发各自的编译流程,将TS代码生成到子包的dist目录。

5. 本地验证发布内容

发布前可执行npx lerna publish --dry-run查看待发布文件列表,确认仅包含dist目录的内容,无TS源码文件。


内容的提问来源于stack exchange,提问作者ben ftima wajdi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 13:47:04