Turborepo internal package WITHOUT Next.js

774 Views Asked by At

I'm trying to create an internal package in a turborepo project. The shared package follows the same setup as the example, and it works as is in the Vite frontend project. However, when importing that module to the backend (Nest + SWC) the dev script fails Unexpected token 'export'.

The built main.js file is trying to require from the shared package, but it's only a typescript file (as in the example). All the examples and other issues are using Next.js, which has a transpilePackages config option that does exactly what I'm trying to achieve. Is there an alternative to transpilePackages that works outside of Next.js? (preferably with SWC)

Things I've tried:

  • including the shared package in tsconfig.json
  • including it in webpack.config.js (changing the exclude pattern of swc-loader to include that folder from node_modules)

There is a very similar issue to mine but it's still unresolved and uses Next.js.

2

There are 2 best solutions below

0
Anna Markova On

Had the same thing, followed https://turbo.build/repo/docs/handbook/publishing-packages/bundling and it helped.
When I was trying to do the same with "build": "tsc" in my package's package.json it didn't work properly, switching to "build": "tsup src/index.ts --format cjs,esm --dts" worked better for both the vite app and the commonjs backend I have.

0
olafurr On

I had the same problem. Including the package in the "include" property in tsconfig.json solved it for me. Just make sure you don't exclude the whole node_modules folder.

{
   "compilerOptions": {...},
   "include": ["node_modules/PROJECT"],
   "exclude": ["node_modules(?!/PROJECT\b)(?:/.*)?"]
}