我得到了错误
[原因]:错误:无法打开文件:“D:\IntelliJ IDEA***\TypeORM_DataSource.ts”。不能在模块外使用 import 语句
尝试运行播种机时出错
typeorm-seeding \
--dataSource 01-Source/Implementation/Elements/DataBase/TypeORM_DataSource.ts \
seed 01-Source/Implementation/Elements/DataBase/Seeders/*.ts
使用@jorgebodega/typeorm-seeding.
好吧,这是ts-node的常见错误,但每次的原因和解决方案都不同。就我而言,将
"type": "module"
添加到package.json
并不能解决问题。
顺便说一下,TypeORM 迁移工作正常:
typeorm-ts-node-esm migration:generate ./01-Source/Implementation/Elements/DataBase/Migrations/Initialization -d ./01-Source/Implementation/Elements/DataBase/TypeORM_DataSource.ts
在文档中,给出了带有
.ts
的例子:
typeorm-seeding seed -d path/to/datasource src/seeders/*.ts
因此,typeorm-seeding 必须有内置的 TypeScript 支持。 最有可能的是,它正在使用 ts-node.
export
/imports
关键字的使用完全是 TypeScript 的基本场景。
它将被转换成什么 - 不同的问题。
typeorm-seeding 不应该关心将输入的 TypeScript 转换为适当类型的模块吗?
理论上,
ts-node
设置应该解决模块类型的问题。
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"strict": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"baseUrl": "./01-Source",
"paths": {
"@CommonSolution/*": [ "./CommonSolution/*"],
/* ... */
}
},
"ts-node": {
"compilerOptions": {
"module": "CommonJS"
},
"require": [
"tsconfig-paths/register"
]
}
}
version: "3.5"
services:
Database:
image: postgres
container_name: Example-Local-Database
ports:
- "${DATABASE_PORT}:${DATABASE_PORT}"
environment:
- "POSTGRES_PASSWORD=${DATABASE_PASSWORD}"
volumes:
- DatabaseData:/var/lib/postgresql/data
# ...
volumes:
DatabaseData:
name: Example-Local-DatabaseData
driver: local