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

TurboRepo单体仓库中Webpack未打包共享包并抛出"Unexpected token 'export'"错误的问题求助

Fixing "Unexpected token 'export'" in TurboRepo + NestJS Webpack Builds

Hey, I’ve run into exactly this issue with a TurboRepo + NestJS setup using pnpm—let me walk you through how I fixed it.

The root cause here is that webpack-node-externals is treating your internal workspace packages (the ones in /packages) as external dependencies by default. That means Webpack isn’t transpiling them, and if those packages output ES module syntax (like export), Node.js (which runs in CommonJS mode by default for NestJS) can’t parse it, hence the SyntaxError.

Here’s what you need to do:

1. Update Your Webpack Externals Configuration

Modify your Webpack config to tell webpack-node-externals to include your internal packages in the build process. Adjust the allowlist to match your workspace package scope (looks like yours starts with @yeo/):

const nodeExternals = require("webpack-node-externals");
const { RunScriptWebpackPlugin } = require("run-script-webpack-plugin");

module.exports = function (options, webpack) {
  return {
    ...options,
    entry: ["webpack/hot/poll?100", options.entry],
    externals: [
      nodeExternals({
        // Allow Webpack to transpile all @yeo/* internal packages
        allowlist: ["webpack/hot/poll?100", /^@yeo\/.+/],
      }),
    ],
    plugins: [
      ...options.plugins,
      new webpack.HotModuleReplacementPlugin(),
      new webpack.WatchIgnorePlugin({ paths: [/\.js$/, /\.d\.ts$/], }),
      new RunScriptWebpackPlugin({ name: options.output.filename, autoRestart: false }),
    ],
  };
};

This regex (/^@yeo\/.+/) ensures all your shared packages get transpiled alongside your NestJS apps, converting ES module syntax to something Node.js can understand.

2. Validate Shared Package TypeScript Configs

Make sure each package in /packages has a tsconfig.json that outputs compatible code. For most NestJS setups, CommonJS is the safest bet:

{
  "compilerOptions": {
    "module": "CommonJS",
    "target": "ES2020",
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

Also, double-check each package’s package.json points to the compiled output:

{
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts"
}

3. Ensure TurboRepo Build Order is Correct

In your root turbo.json, make sure app builds depend on their shared package builds. This ensures Turbo compiles your packages first before building the apps:

{
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**"]
    }
  }
}

The ^build tells Turbo to run the build script on all dependent packages first.

Test the Fix

Run these steps to verify:

  • Clean all existing builds: pnpm exec turbo clean
  • Rebuild everything: pnpm run build
  • Start your app in dev mode: pnpm start:dev

This should resolve the Unexpected token 'export' error and get your Webpack HMR and production builds working as expected.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 16:47:38