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() {}
}
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"]
}