TypeORM이 Cannot import from outside in module
에러를 내면서 migration run
과 migration revert
를 실행시키지 못하는 경우가 있어 tsconfig-paths 모듈을 설치 후 ts-node
를 통해 run
및 revert
를 실행시켜주어야 한다.
yarn add tsconfig-paths
나는 index.ts
에 TypeORM DataSource
가 다음과 같이 정의되어 있는 상태이다.
export const AppDataSource = new DataSource({
type: 'mysql',
...
synchronize: process.env.NODE_ENV === 'dev' ? true : false,
logging: true,
entities: [Video, VideoFile],
migrations: ['./src/migration/*.ts'],
subscribers: [],
});
그리고 package.json
에 다음과 같이 커맨드를 정의하였다.
"migration:create": "yarn typeorm migration:create './src/migration/migrate'",
"migration:run": "yarn ts-node --compilerOptions '{\"experimentalDecorators\": true}' -r tsconfig-paths/register ./node_modules/.bin/typeorm migration:run -d index.ts",
"migration:revert": "yarn ts-node --compilerOptions '{\"experimentalDecorators\": true}' -r tsconfig-paths/register ./node_modules/.bin/typeorm migration:revert -d index.ts"
ts-node
로 실행하는 run
과 revert
의 경우, 같은 경로에 존재하는 tsconfig.json
을 읽지 못하는 경우가 발생하여 compilerOptions
을 직접 명시해주었다.