Ali
Ali

Reputation: 675

turborepo package unexpected token export

I have a custom package named project-types where i share my types between packages

and when i import the simple class into my nestjs project typescript compiles it just fine, but when it comes to runtime and executing the javascript it sais:

server:dev: D:\Program\net-autism\packages\project-types\index.ts:1
server:dev: export * from "./authentication";     
server:dev: ^^^^^^
server:dev: 
server:dev: SyntaxError: Unexpected token 'export'
server:dev:     at Object.compileFunction (node:vm:352:18)
server:dev:     at wrapSafe (node:internal/modules/cjs/loader:1031:15)
server:dev:     at Module._compile (node:internal/modules/cjs/loader:1065:27)
server:dev:     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)      
server:dev:     at Module.load (node:internal/modules/cjs/loader:981:32)

package.json

{
  "name": "project-types",
  "version": "0.0.5",
  "main": "./index.ts",
  "types": "./index.ts",
  "type": "module",
  "license": "MIT",
  "scripts": {
    "lint": "eslint *.ts*"
  },
  "devDependencies": {
    "eslint": "^7.32.0",
    "eslint-config-custom": "*",
    "tsconfig": "*",
    "typescript": "^4.5.2"
  },
  "dependencies": {
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2"
  }
}

usage:

import { Body, Controller, Post } from '@nestjs/common';
import { LoginDto } from 'project-types';

@Controller('auth')
export class AuthController {
  constructor() {}

  @Post('login')
  async login(@Body() credentials: LoginDto) {}

  @Post('register')
  async register() {}
}

Upvotes: 13

Views: 6246

Answers (2)

Rafik Belkadi
Rafik Belkadi

Reputation: 394

The better solution is to do what @Patrick Kenyon did,

and add in your ts-config this :

Your package tsconfig.json :

   "compilerOptions":{
   ...
   "paths": {
     "your_package_name": ["./src/index"]
   }

Your App tsconfig.json

   "compilerOptions":{
   ...
    "paths": {

           "dto": ["../../../packages/your_package_name/src/index.ts"]
       }

Upvotes: 1

Patrick Kenyon
Patrick Kenyon

Reputation: 311

I've also just come up against this problem. From what I can tell when you build your consuming app it isn't transpiling the Typescript from your types package.

For NextJS projects there is an npm package which addresses this issue https://www.npmjs.com/package/next-transpile-modules

However, as I have a NestJS project you'll need to transpile the types package yourself. This is my set up:

packages/project-types/package.json

{
  ...
  // entry point is now the transpiled version of your code
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "type": "commonjs",
  "scripts": {
    "build": "tsc",
  ...
}

packages/project-types/tsconfig.json

{
  "include": ["."],
  "exclude": ["dist", "build", "node_modules"],
  "compilerOptions": {
    "module": "commonjs",
    "outDir": "./dist"
  }
}

apps/nest-project/tsconfig.json

{
  ...
  "module": "commonjs",
  ...
}

You can then configure your turbo.json to ensure that all dependent packages are built beforehand topological dependencies. Example taken from the Turbo docs:

  "pipeline": {
    "build": {
      // "A package's `build` command depends on its dependencies'
      // and devDependencies' `build` commands being completed first"
      "dependsOn": ["^build"]
    }
    // ... omitted for brevity
  }

Hope that helps.

[Update]

Example repo

Upvotes: 18

Related Questions