nx: tsconfig target incompatibility with NestJS and TypeORM module
Prerequisites
- I am running the latest version
- I checked the documentation and found no answer
- I checked to make sure that this issue has not already been filed
- I’m reporting the issue to the correct repository (not related to Angular, AngularCLI or any dependency)
Expected Behavior
- tsconfig target should be es6 when scaffolding a new node application : https://github.com/nestjs/nest/issues/2190#issuecomment-492150156
- nestjs/typeorm should be able to load entity files and execute migration files without any errors
Current Behavior
Given a tsconfig file with es6 target and the following typeorm config :
{
...
entities: ['apps/api/**/**.entity.ts'],
migrations: ['apps/api/migrations/*.ts'],
migrationsRun: true
...
}
When I start the nestjs application I get the following error when typeorm tries to load the entities
[Nest] 21470 - 2019-05-25 10:18 [NestFactory] Starting Nest application...
[Nest] 21470 - 2019-05-25 10:18 [InstanceLoader] AppModule dependencies initialized +129ms
[Nest] 21470 - 2019-05-25 10:18 [InstanceLoader] CoreModule dependencies initialized +9ms
[Nest] 21470 - 2019-05-25 10:18 [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 21470 - 2019-05-25 10:18 [InstanceLoader] PubSubModule dependencies initialized +2ms
[Nest] 21470 - 2019-05-25 10:18 [InstanceLoader] PassportModule dependencies initialized +7ms
[Nest] 21470 - 2019-05-25 10:18 [InstanceLoader] JwtModule dependencies initialized +1ms
[Nest] 21470 - 2019-05-25 10:18 [InstanceLoader] GraphQLModule dependencies initialized +1ms
[Nest] 21470 - 2019-05-25 10:18 [TypeOrmModule] Unable to connect to the database. Retrying (1)... +152ms
/Users/jonjon/work/nx/typefaster/apps/api/src/app/features/content/content.entity.ts:1
(function (exports, require, module, __filename, __dirname) { import * as GQL from '@tf/apps/api/typings/graphql';
^
SyntaxError: Unexpected token *
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
Then I tried to use entity classes directly instead of putting the paths :
entities: ['apps/api/**/**.entity.ts'] -->
entities: [UserEntity, ContentEntity, RandomEntity, RecordEntity]
Now I get the following error but for typeorm migrations :
[Nest] 21543 - 2019-05-25 10:27 [NestFactory] Starting Nest application...
[Nest] 21543 - 2019-05-25 10:27 [InstanceLoader] AppModule dependencies initialized +155ms
[Nest] 21543 - 2019-05-25 10:27 [InstanceLoader] CoreModule dependencies initialized +1ms
[Nest] 21543 - 2019-05-25 10:27 [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 21543 - 2019-05-25 10:27 [InstanceLoader] PubSubModule dependencies initialized +1ms
[Nest] 21543 - 2019-05-25 10:27 [InstanceLoader] PassportModule dependencies initialized +1ms
[Nest] 21543 - 2019-05-25 10:27 [InstanceLoader] JwtModule dependencies initialized +16ms
[Nest] 21543 - 2019-05-25 10:27 [InstanceLoader] GraphQLModule dependencies initialized +1ms
[Nest] 21543 - 2019-05-25 10:27 [TypeOrmModule] Unable to connect to the database. Retrying (1)... +171ms
/Users/jonjon/work/nx/typefaster/apps/api/migrations/1551728841387-Migration.ts:1
(function (exports, require, module, __filename, __dirname) { import {MigrationInterface, QueryRunner} from "typeorm";
^
SyntaxError: Unexpected token {
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
Context
Please provide any relevant information about your setup:
- version of Nx used : 7.8.6
- version of Angular CLI used : 7.3.1
- version of Angular DevKit used : 0.13.9
- 3rd-party libraries and their versions “@nestjs/common”: “^6.2.4” “@nestjs/core”: “^6.2.4” “@nestjs/typeorm”: “^6.1.1”
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 29
- Comments: 49 (14 by maintainers)
This fixed it for me:
import { Module } from '@nestjs/common';import { TypeOrmModule } from '@nestjs/typeorm';import { getMetadataArgsStorage } from 'typeorm';import { typeOrmConfig } from '../config/typeorm.config';@Module({imports: [...,TypeOrmModule.forRoot({...typeOrmConfig,entities: getMetadataArgsStorage().tables.map(tbl => tbl.target)})]})export class AppModule {}You can also “fix” this issue by instead of supplying globs to the
entitiesarray, you supply the entities directly.However this would require extra steps to introduce an entity. (Can be eased with using an index.ts and combining exports)
I fixed this by changing my entities in my ormconfig.ts
from:
entities: ['apps/**/*.entity.ts']to:
entities: [join(__dirname, './**/*.entity{.ts,.js}')],Make sure you have
import { join } from 'path';Up
My workaround works like this for run CLI command and connect entities from api code:
at the root folder I put a ormconfig.json for the CLI
app.module.ts
The really important here is you need to “overwrite” the values in app.module, if not the config from the ormconfig.json will not work. autoLoadEntities works like a charm here.
npm script to run typeorm CLI
and finally the libs/typeorm/tsconfig.json
Works only with module commonjs
Hope this help.
I’m also experiencing this issue. Have there been any update on this?
thank you very much, you save my days!
Talked to @FrozenPandaz about it. The issue is because we bundle the nest app.
We have a plan in mind. It should be addressed by Nx 9.
I think I found a solution to use TypeOrm Cli without the need to build the library
I used to have my DB entities and migrations as a buildable library and to use TypeOrm Cli I used to build and configure ormconfig.json to dist folder. another problem was that it didn’t let me import from other libraries.
With this solution, it works with TypeOrm CLI to generate the migration and it’s without the need for a buildable library and also Nestjs can run migrations too.
tsconfig-paths: ^3.12.0“typeorm”: “^0.2.41” “ts-node”: “~9.1.1”tsconfig.typeorm.jsonin the root of the library next to other configsLib.LoHi.Backend.Persistence/tsconfig.typeorm.jsonand addedts-nodepart to support paths. (note that it’s extending base tsconfig so it can read paths)package.jsonthis was for TypeOrm CLI to work with paths and typescript files.
the NestJs part is easy.
create two arrays, one for migrations classes and one for entities, and use them in forRoot config. I think these fields are possible with path (I didn’t test and don’t recommend, you can create a script to go through your entities and migrations and create the exports file.)
Dear everyone in this thread,
i managed to solve the
migrationsissue in my nrwl/nx application. This is, what i did:(optional!) install
scripty(https://github.com/testdouble/scripty), which lets you execute shell-scripts from npm;npm install --save-dev scripty. While this step is optional, it makes your life easieradd a few
npm scriptsto yourpackage.jsonfile to easily trigger the commands:Note that those npm scripts just call the previous mentioned
scriptylibrary.scriptyshell scripts: in your root folder (next to thepackage.jsonfile) create ascriptsfolder. This folder is “crawled” byscriptywhen invoking a npm script. For example, if your command is calledmigrate:create(see step 2), you need to create a file inscripts/migrate/create.sh.scripts/migrate/create.sh
scripts/migrate/run.sh
ormconfig.jsfile into the root directory of your application that works with a database. As your mono-repository may contain multiple apps that interact with databases, you may want to have differentormconfig.jsfiles.apps/api/src/ormconfig.js
This will create a new file
apps/api/src/database/migrations/src/123456789-CreateUsersTable.ts. You can now fill this migration with life!This command will first transpile all existing
apps/api/src/database/migrations/src/*.tsinto javascript and store them inapps/api/src/database/migrations/generated. The latter are then executed by typeorm.I hope this solution works for you guys!
Hi folks! I’ve reclassified this issue as an enhancement as the solution we’re going to pursue is creating a builder for running
ts-node.The reason that the repro provided by @beeman (thanks!) doesn’t work is that the node process can’t read ts files directly. Providing a
ts-nodebuilder should mean that this approach will work in future.If you can’t wait for the builder implementation, I recommend that, for the moment, you try one of the workarounds which involve importing your entity files so that they don’t get referenced as uncompiled typescript.
Any updates on this issue?
@kuccilim Im having same issue, did You fix it?
According to the docs https://docs.nestjs.com/techniques/database all you enter is
entities: [__dirname + '/**/*.entity{.ts,.js}'],Which gives an error noting that it cannot find any of your entity.ts files
No repository for "Post" was found. Looks like this entity is not registered in current "default" connection?Dumping __dirname reveals it is running in the
dist/app/xyzfolder, which means you would have to useentities: [__dirname + '/../../../app/xyz/src/app/**/*.entity{.ts,.js}'],But I can confirm that @NailRode suggestion of
entities: getMetadataArgsStorage().tables.map(tbl => tbl.target)works for me.I found that inside generated
dist/main.jsfile don’t have any typeorm migration code. How to load migrations file after build?Should I tsc migration files and copy into dist folder?