Typescript&TypeORM:不能在模块外使用import语句

我已经使用 TypeORM 设置了一个 Typescript 项目,但在编译时遇到了一些问题。

我的包结构是这样的:

root
??? db
?   ??? migrations
?   ?   ??? a_migration.ts
?   ??? connection
?   ?   ??? config.ts <- ormconfig
?   ?   ??? encrypt.ts
?   ?   ??? index.ts <- creates the connection
??? src
?   ??? card
?   ?   ??? entity.ts
??? package.json
??? tsconfig.json
root
??? db
?   ??? migrations
?   ?   ??? a_migration.ts
?   ??? connection
?   ?   ??? config.ts <- ormconfig
?   ?   ??? encrypt.ts
?   ?   ??? index.ts <- creates the connection
??? src
?   ??? card
?   ?   ??? entity.ts
??? package.json
??? tsconfig.json

我的 config.ts 是:

tsconfig.json:


export = {
  type: 'postgres',
  host: POSTGRES_HOST,
  port: POSTGRES_PORT,
  username: POSTGRES_USER,
  password: POSTGRES_PASS,
  database: POSTGRES_DB,
  synchronize: true,
  logging: false,
  entities: ['**src/**/*entity.{ts,js}'],
  migrations: ['**/migrations/*.{ts,js}'],
  cli: {
    entitiesDir: 'src/entity',
    migrationsDir: 'db/migrations',
  },
  namingStrategy: new SnakeNamingStrategy(),
};

我尝试用+替换**实体和迁移中的前缀,但是 typeorm 无法检测到迁移文件和实体。我知道这与解析代码在文件夹下的路径有关,但我不确定如何解决这个问题。path.join__dirnamebuild

如果我像这样生活,CLI 适用于执行应用程序的未编译代码 (ts),nodemon但不适用于已编译的 (js) 代码。

我从编译的 js 中得到的错误是:
SyntaxError: Cannot use import statement outside a module

任何帮助表示赞赏,非常感谢!

以上是Typescript&amp;TypeORM:不能在模块外使用import语句的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>