Nestjs 错误导入 "reflect-metadata" 无法在模块外使用导入语句
Nestjs error import "reflect-metadata" Cannot use import statement outside a module
现在我遇到了问题。当我添加多对多关系时,它不起作用。但是在一对多关系之前。
- 导入“reflect-metadata”时出错 无法在模块外使用导入语句
- error ReferenceError: exports is not defined in ES module scope
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[];
}
在文件中 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) ....
当我将 "type": "module" 添加到 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 ....
我试了很多方法都解决不了,请帮帮我
检查你的导入,它应该是 import {...} from 'typeorm'
。避免从 'typeorm/browser'
.
导入
现在我遇到了问题。当我添加多对多关系时,它不起作用。但是在一对多关系之前。
- 导入“reflect-metadata”时出错 无法在模块外使用导入语句
- error ReferenceError: exports is not defined in ES module scope
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[];
}
在文件中 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) ....
当我将 "type": "module" 添加到 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 ....
我试了很多方法都解决不了,请帮帮我
检查你的导入,它应该是 import {...} from 'typeorm'
。避免从 'typeorm/browser'
.