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

Yarn Workspaces单仓NestJS API编译公共模块报export语法错误

Yarn Workspaces单仓中NestJS调用公共子项目报SyntaxError: Unexpected token 'export'

我正在搭建一个包含NestJS API、React应用及多个共享子项目的单仓,采用Yarn Workspaces进行管理。React应用通过Vite可以正常导入并编译公共子项目,但NestJS API始终无法完成编译,频繁抛出SyntaxError: Unexpected token 'export'错误。

我已创建最小复现环境,目录结构如下:

.
├── common                  # 示例公共项目
│   ├── tsconfig.json       
│   ├── src        
│   │   └── test.ts         # 被API调用的文件
│   └── package.json   
├── api                     # NestJS API
│   ├── tsconfig.json      
│   ├── src        
│   └── package.json             
└── package.json            # Yarn Workspaces配置文件

核心配置文件

根目录package.json

{
    "name": "mono",
    "workspaces": [
        "common",
        "api"
    ]
}

公共子项目@test/common

package.json

{
    "name": "@test/common",
    "version": "1.0.0",
    "main": "index.ts"
}

tsconfig.json

{
  "compilerOptions": {
    "module": "CommonJS",
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "esModuleInterop": true,
    "composite": true,
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true
  }
}

src/test.ts

export const hi = () => 'Hello there';

NestJS API项目

src/main.ts调用代码

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { hi } from '@test/common';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);

  console.log('All fine', hi());
}
bootstrap();

package.json

{
  "name": "api",
  "scripts": {
    "start": "nest start",
    ...other commands
  },
  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@test/common": "*",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "webpack-node-externals": "^3.0.0"
  },
  "devDependencies": {
    ...nest相关依赖
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  },
  "jest": {
    ...jest配置
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "CommonJS",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false
  },
  "include": [
    "../common",
    "./src/*"
  ],
  "references": [
    {
      "path": "../common"
    }
  ],
  "paths": {
    "@test/common/*": [
      "../common/*"
    ]
  }
}

错误现象

在Nest项目中运行yarn run start时,生成的dist目录结构看似正常,但仍会触发以下错误:

C:\<path>\MonoNest\api>yarn run start
yarn run v1.22.19
$ nest start
C:\<path>\MonoNest\common\index.ts:1
export * from './src/test';
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (C:\<path>\MonoNest\api\src\main.ts:3:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

需求说明

需要构建一个「真正的」单仓,仅在根目录维护各项目的路径引用,不采用NestJS官方单仓方案(该方案会在根仓库引入大量我不希望其他项目依赖的Nest包及配置)。

内容的提问来源于stack exchange,提问作者Treycos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 02:55:17