Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

448
Views
Nestjs error import "reflect-metadata" Cannot use import statement outside a module

Right now I'm having a problem. When i add relationship many-to-many it's not working. But before with relation one-to-many.

  • error import "reflect-metadata" Cannot use import statement outside a module
  • error ReferenceError: exports is not defined in ES module scope

My pull request

user.entity.ts

@Entity({ name: 'users' })
export class User implements IUser {
  @PrimaryGeneratedColumn()
  id: string;

  @Column({ length: 25, nullable: true })
  name: string;

  @Column({ unique: true, length: 255 })
  email: string;

  @OneToMany(() => Message, (message) => message.user)
  messages?: Message[];

  @ManyToMany(() => Conversation, (conversations) => conversations.users)
  @JoinTable({
    name: 'user_conversation',
    joinColumn: { name: 'user_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'conversation_id' },
  })
  conversations: Conversation[];
}

in file conversation.entity.ts

@Entity({ name: 'conversations' })
export class Conversation implements IConversation {
  @PrimaryGeneratedColumn()
  id: string;

  @Column({ name: 'title', nullable: true })
  title: string;

  @ManyToMany(() => User, (users) => users.conversations)
  @JoinTable({
    name: 'user_conversation',
    joinColumn: { name: 'conversation_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'user_id' },
  })
  users: User[];
}

my error :

import "reflect-metadata";
SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16) ....

When i add "type": "module" to package.json have error

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and ....

I tried many ways but can't solve it, please help me.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Check your imports, it should be import {...} from 'typeorm'. Avoid imports from 'typeorm/browser'.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!