Monday, August 24, 2020

Solve Nestjs TypeORM entity import problem.

 

Version 6 of Nestjs has introduced some new features in cli. This cli performs many tasks properly. However it has also introduced some new problems. One of which is TypeORM entity not registered problem. Until Nestjs community fixes this issue there is work around I have found.

This blog is written to solve following error-

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
^
SyntaxError: Unexpected token {

You can use previous version (5) of nestjs cli but if you don’t want to then follow following instructions-

For database connection using TypeORM- use nodemon config and update start, start:dev, and start:debug scripts.

create nodemon.json with following content

{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node -r tsconfig-paths/register src/main.ts"
}

create nodemon-debug.json with following content

{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts"
}

npm scripts required-

"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "nodemon",
"start:debug": "nodemon --config nodemon-debug.json",

I hope your problem has fixed and you can perform table creation using TypeORM entities in Nestjs.

This blog written by me on medium