Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeORM migration:generate not recognise import baseurl

I am using typeORM with nestjs and typescript. I have nestjs so all import statements can start with 'src/...' typeorm will only accept imports to other entities using ../

Not finding the referenced entity

import { User } from 'src/users/entities/user.entity';

Structure to finding the referenced entity:

import { User } from '../users/entities/user.entity';

I am using the script from package.json

"migration:generate": "ts-node node_modules/.bin/typeorm migration:generate -n"

ormconfig.json

[
 {
  "name": "default",
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "xxxx",
  "password": "xxx",
  "database": "xxxx",
  "schema": "xxx",
  "migrations": ["dist/migrations/*{.ts,.js}"],
  "migrationsTableName": "migrations_typeorm"
 }
]

tsconfig.json

{
 "compilerOptions": {
   "module": "commonjs",
   "declaration": true,
   "removeComments": true,
   "emitDecoratorMetadata": true,
   "experimentalDecorators": true,
   "allowSyntheticDefaultImports": true,
   "target": "es2017",
   "sourceMap": true,
   "outDir": "./dist",
   "baseUrl": "./",
   "incremental": true
  }
 }

nest-cli.json

{
 "collection": "@nestjs/schematics",
 "sourceRoot": "src"
}

I have tried the suggestion typeorm migration api does not generate automatic code but it does not solve my issue. How do I get orm migration to accept a path starting with src/ in the import statement when including other entities?

Many thanks

like image 465
Chris Hunter-Johnson Avatar asked Oct 28 '25 07:10

Chris Hunter-Johnson


1 Answers

I encountered this problem for 2 days. I've been scratching my head why typeorm cli does not respect the 'baseUrl' setting in my tsconfig.json. I think i found the solution. So if you look at another script in the package.json:

"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand"

there is this module called tsconfig-paths/register. I searched about it, and bingo! It does help with resolving import path while respecting 'baseUrl' in tsconfig.json. So i modified my typeorm script in package.json like this:

"typeorm": "node -r tsconfig-paths/register -r ts-node/register ./node_modules/typeorm/cli.js"

And then it works like a charm. Thanks.

like image 168
Rooco Avatar answered Oct 31 '25 11:10

Rooco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!