Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nest.js build has invalid output: Cannot access X before initialization #6016

Closed
Jawell opened this issue Oct 2, 2022 · 2 comments
Closed
Labels

Comments

@Jawell
Copy link

Jawell commented Oct 2, 2022

Describe the bug

Compilation was success, but node dist/main.js failed with:
get: ()=>User
^
ReferenceError: Cannot access 'User' before initialization

Dependencies:
"typeorm": "^0.3.5",
"@nestjs/typeorm": "^8.0.3",
"@nestjs/common": "^8.4.4",
"@nestjs/cli": "^8.2.5",
"@swc/cli": "^0.1.57",
"@swc/core": "^1.3.4",

Input code

Base.entity.ts

import { Column, BeforeUpdate, PrimaryGeneratedColumn } from 'typeorm';

export abstract class BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({
    name: 'created_at',
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
  })
  public createdAt: Date | null;

  @Column({
    name: 'updated_at',
    type: 'timestamp',
    nullable: true,
  })
  public updatedAt: Date | null;

  @BeforeUpdate()
  public setUpdatedAt() {
    this.updatedAt = new Date();
  }
}

User.entity.ts

import { Column, Entity, OneToMany } from 'typeorm';

import { BaseEntity } from '../../Base.entity';
import { Material } from '../material/material.entity';

@Entity()
export class User extends BaseEntity {
  @Column({
    type: 'varchar',
    length: 64,
    transformer: {
      to: (value: string) => value.toLowerCase(),
      from: (value: string) => value,
    },
  })
  public firstName: string;

  @Column({
    type: 'varchar',
    length: 64,
    transformer: {
      to: (value: string) => value.toLowerCase(),
      from: (value: string) => value,
    },
  })
  public lastName: string;

  @Column({
    type: 'varchar',
    length: 128,
    unique: true,
  })
  public email: string;

  @Column({
    type: 'varchar',
    length: 81,
    select: false,
  })
  public password: string;

  @OneToMany(() => Material, (material) => material.author)
  public materials: Material[];
}

Material.entity.ts

import { Entity, Column, ManyToOne } from 'typeorm';

import { BaseEntity } from '../../Base.entity';
import { User } from '../user/user.entity';

@Entity()
export class Material extends BaseEntity {
  @Column({
    type: 'varchar',
    length: 64,
  })
  public title: string;

  @Column({
    type: 'json',
  })
  public description: object;

  @Column({
    type: 'varchar',
    length: 24,
    nullable: true,
  })
  public attachment: string;

  @Column({
    name: 'published_at',
    type: 'timestamp',
    nullable: true,
  })
  public publishedAt: Date | null;

  @ManyToOne(() => User, (user) => user.materials)
  public author: User;
}

Config

{
    "jsc": {
        "loose": true,
        "target": "es2022",
        "parser": {
            "syntax": "typescript",
            "decorators": true,
            "dynamicImport": true
        },
        "transform": {
            "legacyDecorator": true,
            "decoratorMetadata": true
        }
    },
    "module": {
        "type": "commonjs",
        "strict": false,
        "strictMode": false,
        "lazy": true,
        "noInterop": true
    }
}

Playground link

No response

Expected behavior

Runnable script

Actual behavior

No response

Version

1.3.4

Additional context

No response

@Jawell Jawell added the C-bug label Oct 2, 2022
@kdy1
Copy link
Member

kdy1 commented Oct 2, 2022

#5047

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2022
@kdy1 kdy1 added invalid and removed C-bug labels Oct 2, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Nov 1, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants