question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"ReferenceError: Cannot access 'Entity' before initialization" when running migrations

See original GitHub issue

Issue type:

[x] question [x] bug report? [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.24

Steps to reproduce or a small repository showing the problem:

Getting the following error when trying to add relations and then generate migrations:

Error during migration generation:
ReferenceError: Cannot access 'B' before initialization

Even with this incredibly simple example (classes in the same file):

@Entity()
export class A extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  public id!: string;

  @OneToOne(
    () => B,
    b => b.a
  )
  public b!: B;
}

@Entity()
export class B extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  public id!: string;

  @OneToOne(
    () => A,
    a => a.b
  )
  @JoinColumn()
  public a!: A;
}

This code compiles ~and runs~ fine, I just cannot run migrations for it.

Edit: I get the same error when starting my server.

My orm config is something like:

import { ConnectionOptions } from 'typeorm';

import * as entities from './entities';

export const ORM_CONFIG: ConnectionOptions = {
  type: 'postgres',
  url: process.env.DATABASE_URL,
  entities: Object.values(entities),
  logging: true,
};

I had to import my entities because I haven’t yet been able to get typeorm to pick them up at all when using paths; for example, ./entities/**/*.ts.

And here’s my tsconfig:

{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "pretty": true,
    "sourceMap": true,
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "jsx": "react",
    "target": "es6",
    "moduleResolution": "node",
    "typeRoots": ["./node_modules/@types/", "./@types/"]
  },
  "include": ["./src/", "./@types/"]
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:11

github_iconTop GitHub Comments

2reactions
cacabocommented, Jun 29, 2020
2reactions
JakeSidSmithcommented, May 5, 2020

Okay, this now appears to work if I put the entities into separate modules and change my orm config to:

import * as fs from 'fs';
import * as path from 'path';
import { ConnectionOptions } from 'typeorm';

const ENTITIES_DIR = path.resolve(__dirname, 'entities');

const entities = fs
  .readdirSync(ENTITIES_DIR)
  .map(file => path.resolve(ENTITIES_DIR, file));

export const ORM_CONFIG: ConnectionOptions = {
  type: 'postgres',
  url: process.env.DATABASE_URL,
  entities,
  logging: true,
};

But this isn’t ideal. Can I not have my entities in the same file?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot access '<Entity>' before initialization" - Stack Overflow
I tried to get a vue electron app to work with typeorm. Error was: ReferenceError, can not use variable before declaration on an...
Read more >
referenceerror: cannot access 'main' before initialization
The “cannot access before initialization” reference error occurs in JavaScript when you try to access a variable before it is declared with let...
Read more >
Getting TypeORM to work with Next.js and TypeScript
Did you experience this error? ReferenceError: Cannot access 'User' before initialization. I'm having trouble getting past this.
Read more >
“How-to” Guides - Spring
Thus, ServerProperties has prefix="server" and its configuration properties are server.port , server.address , and others. In a running application with ...
Read more >
NextJS + TypeORM 設定 - Qiita
Copied! ReferenceError: Cannot access 'User' before initialization ... "yarn typeorm migration:generate -n", "db:migrate:run": "yarn typeorm ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found