【问题标题】:Error when using TypeORM, and Typescript with folder path使用 TypeORM 和带有文件夹路径的 Typescript 时出错
【发布时间】:2021-06-18 11:12:19
【问题描述】:

将 TypeScript 与 TypeORM 结合使用并将实体设置为 .js 文件的文件路径时,出现以下错误:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:...\Projects\orders-api\bin\models\entities\Address.js
require() of ES modules is not supported.
require() of ...\Projects\orders-api\bin\models\entities\Address.js from ...\Projects\orders-api\node_modules\typeorm\util\DirectoryExportedClassesLoader.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename Address.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from c:\Users\mrivera\Projects\orders-api\package.json.

我正在使用 Typescript - 4.2.3,TypeORM - 0.2.31

这是我的 tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module":"ESNext",
    "esModuleInterop": true,
    "moduleResolution": "node",
    "strict": true,
    "declaration": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "outDir": "bin",
    "types": ["node"],
    "typeRoots": ["../node_modules/@types"],
    "sourceMap": true
  },
  "include": ["**/*.ts"],
  "exclude": ["tests", "node_modules", "lib", "bin", "dist"]
}

和 package.json

{
  "name": "test-api",
  "version": "0.0.12",
  "description": "...",
  "license": "ISC",
  "author": "Me",
  "main": "server.js",
  "type": "module",
  "scripts": {
    "start": "node bin/server.js",
    "build": "npm i && tsc",
    "lint": "./node_modules/.bin/eslint --ignore-path .gitignore .",
    "test": "echo \"Error: currently no test specified\""
  },
  "dependencies": {
    "express": "^4.17.1",
    "mssql": "^6.3.1",
    "reflect-metadata": "^0.1.13",
    "syswide-cas": "^5.3.0",
    "typeorm": "^0.2.31",
    "uuid-mongodb": "^2.4.2"
  },
  "devDependencies": {
    "@types/node": "^14.14.35"
  }
}

我不使用 ormconfig.json 文件,因为我必须在应用程序运行时设置配置值。 以下是我的连接选项的示例:

return createConnection({
    name: "default",
    type: "mssql",
    host: this._server,
    username: this._user_name,
    password: this._password,
    database: this._database,
    extra: true,
    options: {
        isolation: "READ_UNCOMMITTED",
    },
    entities: [
        `${this._entity_path}/entities/*.js`
    ],
})

我在使用调试器的过程中验证了实体路径是正确的。

为了提供帮助,这是我的一个实体 (Address.ts) 的示例:

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

import { Patient } from './Patient.js';
import { PatientNextOfKin } from './PatientNextOfKin.js';

@Index("PK__Address__3213E83F7240D5FD", ["id"], { unique: true })
@Entity("Address", { schema: "Orders" })
export class Address {
  @PrimaryGeneratedColumn({ type: "int", name: "id" })
  id!: number;

  @Column("nvarchar", { name: "line", length: 500 })
  line!: string;

  @Column("nvarchar", { name: "line2", length: 100 })
  line2!: string;

  @Column("nvarchar", { name: "city", length: 100 })
  city!: string;

  @Column("nvarchar", { name: "state", length: 2 })
  state!: string;

  @Column("nvarchar", { name: "zip", length: 10 })
  zip!: string;

  @OneToMany(() => Patient, (patient) => patient.address)
  patients!: Patient[];

  @OneToMany(
    () => PatientNextOfKin,
    (patientNextOfKin) => patientNextOfKin.address
  )
  patientNextOfKins!: PatientNextOfKin[];
  
}

使用 tsc 命令将代码转换为 bin 文件夹中的 .js 文件

【问题讨论】:

  • 您可以将 Address.js 重命名为 Address.ts,因为您使用的是 Typescript?
  • 代码是用打字稿写的。代码被编译成 .js 文件,放在 bin 文件夹中。
  • 你找到解决方案了吗?
  • @PeterNgesh:我尝试了各种不同的方法,例如使用桶文件。我什么也做不了。我最终使用了不同的库,因为我找不到这个问题的解决方案。
  • @thxmike 谢谢。我能够找出我的问题。我将在这里发布我的解决方案

标签: typescript typeorm


【解决方案1】:

我的应用也有同样的问题,可能的解决方法类似于this answer 中的建议:

在您的entities 文件夹中,您可以创建一个index.ts

export * from './Patient.js';
export * from './PatientNextOfKin.js';
export * from './Address.js';

您的createConnection 将变为:

import * as entities from "./entities/index.js";

//...

return createConnection({
    // ...
    entities: [
        ...Object.values(entities)
    ],
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多