首页 > 解决方案 > Nestjs错误导入“reflect-metadata”无法在模块外使用导入语句

问题描述

现在我有一个问题。当我添加多对多关系时,它不起作用。但之前是一对多的关系。

我的拉取请求

用户实体.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[];
}

在文件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[];
}

我的错误:

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

当我添加“类型”:“模块”到 package.json 有错误

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 ....

我尝试了很多方法,但无法解决,请帮助我。

标签: javascriptnode.jsnestjstypeorm

解决方案


检查你的进口,它应该是import {...} from 'typeorm'. 避免从'typeorm/browser'.


推荐阅读