0
votes

i'am configuring nestjs ormconfig.json but typeorm don't connect to mysql 8 database, it display this in cli

[Nest] 13324   - 2020-01-14 4:15:32   [NestFactory] Starting Nest application...
[Nest] 13324   - 2020-01-14 4:15:32   [InstanceLoader] AppModule dependencies initialized +513ms
[Nest] 13324   - 2020-01-14 4:15:32   [InstanceLoader] TypeOrmModule dependencies initialized +5ms
[Nest] 13324   - 2020-01-14 4:15:36   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +4061ms
Error: Cannot find module 'src/user/user.entity'
Require stack:
- C:\Users\redwolf\Labs\projets-pro\kissing-api\src\appel\appel.entity.ts
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\typeorm\platform\PlatformTools.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\typeorm\driver\sqlserver\SqlServerDriver.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\typeorm\migration\MigrationExecutor.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\typeorm\connection\Connection.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\typeorm\connection\ConnectionManager.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\typeorm\index.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\@nestjs\typeorm\dist\common\typeorm.utils.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\@nestjs\typeorm\dist\common\typeorm.decorators.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\@nestjs\typeorm\dist\common\index.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\@nestjs\typeorm\dist\index.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\@nestjs\typeorm\index.js
- C:\Users\redwolf\Labs\projets-pro\kissing-api\src\app.module.ts
- C:\Users\redwolf\Labs\projets-pro\kissing-api\src\main.ts
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
    at Function.Module._load (internal/modules/cjs/loader.js:690:27)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\Users\redwolf\Labs\projets-pro\kissing-api\src\appel\appel.entity.ts:3:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Module.m._compile (C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\ts-node\src\index.ts:806:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\redwolf\Labs\projets-pro\kissing-api\node_modules\ts-node\src\index.ts:809:12)
    at Module.load (internal/modules/cjs/loader.js:815:32)

all entities are in src folder but the console still say the're not

and this is my code

app.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
      TypeOrmModule.forRoot(),
  ],
})
export class AppModule {}

ormconfig.json

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "admin",
  "password": "",
  "database": "kissing_db",
  "synchronize": true,
  "logging": true,
  "entities": [
    "src/**/*.entity.ts",
    "dist/**/*.entity.js"
  ],
  "migrationsTableName": "migration",
  "migrations": [
    "src/migration/*.ts"
  ],
  "cli": {
    "migrationsDir": "./src/migration"
  },
  "ssl": false
}

I don't post it with password for security reason hope you'll understand.

2

2 Answers

1
votes

When compiling from Typescript to JavaScript, your code gets moved from .ts files in the src directory to .js files in the dist directory. From there, Node still tries to resolve the paths given from your base directory (once in dist the base is dist), so it tries to resolve dist/src/user/user.entity which obviously doesn't exist. Instead, you should remove the src/**/*.entity.ts from the ormconfig.json so that it will only look for compiled JavaScript files.

There's a bit more information here.

0
votes

I think i've found a solution i've just create a ormconfig.js withouth entities field like this

module.exports = {
   "type": "mysql",
   "host": "localhost",
   "port": 3306,
   "username": "admin",
   "password": "",
   "database": "kissing_db",
   "synchronize": true,
   "logging": true,
};

and edit my package.json start scripts with ts-node, it seem like ts-node automatically find entity files.