【发布时间】:2021-08-05 07:41:23
【问题描述】:
我已经使用 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
我的 config.ts 是:
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(),
};
tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"target": "es6",
"module": "CommonJS",
"allowJs": false,
"esModuleInterop": true,
"noImplicitAny": true,
"resolveJsonModule": true,
"outDir": "./build",
"strict": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"include": ["./src/*", "./db/*"],
"exclude": ["./**/__tests__/*", "./**/__functionaltests__/*"]
}
我尝试用 path.join + __dirname 替换实体和迁移中的 ** 前缀,但 typeorm 无法检测到迁移文件和实体。我知道这与解析代码在build 文件夹下的路径有关,但我不确定如何解决这个问题。
如果我这样生活,CLI 适用于执行应用程序的未编译代码 (ts),例如 nodemon,但不适用于已编译 (js) 代码。
我从编译的 js 中得到的错误是:
SyntaxError: Cannot use import statement outside a module
感谢任何帮助,非常感谢!
【问题讨论】:
标签: javascript node.js typescript postgresql typeorm