Mikro-orm: MetadataProvider can't find entities in directory

Created on 20 Sep 2019  路  3Comments  路  Source: mikro-orm/mikro-orm

Describe the bug
When I'm trying to run my app via node -r tsconfig-paths/register path/to/myapp.js I got the excepetion below.

Stack trace

[2019-09-20 20:41:49.306 +0000] TRACE (7168 on kgoncharov-linux): MikroORM MikroORM: successfully connected to database db on mysql://db-name:*****@localhost:3306
[2019-09-20 20:41:49.318 +0000] ERROR (7168 on kgoncharov-linux): ExceptionHandler Cannot read property 'properties' of undefined TypeError: Cannot read property 'properties' of undefined
    at JavaScriptMetadataProvider.loadEntityMetadata (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/mikro-orm/dist/metadata/JavaScriptMetadataProvider.js:11:31)
    at MetadataDiscovery.discoverEntity (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/mikro-orm/dist/metadata/MetadataDiscovery.js:89:37)
    at async MetadataDiscovery.discoverDirectory (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/mikro-orm/dist/metadata/MetadataDiscovery.js:72:13)
    at async Function.runSerial (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/mikro-orm/dist/utils/Utils.js:187:22)
    at async MetadataDiscovery.discover (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/mikro-orm/dist/metadata/MetadataDiscovery.js:37:13)
    at async Function.init (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/mikro-orm/dist/MikroORM.js:13:28)
    at async Injector.instantiateClass (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/@nestjs/core/injector/injector.js:280:37)
    at async callback (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/@nestjs/core/injector/injector.js:74:30)
    at async Injector.resolveConstructorParams (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/@nestjs/core/injector/injector.js:113:24)
    at async Injector.loadInstance (/home/kgoncharov/Projects/PROJECT/be/dist/node_modules/@nestjs/core/injector/injector.js:78:9)

To Reproduce
Parts of the config:

          entitiesDirs: ['/entities'],
          entitiesDirsTs: ['./entities'],
          autoFlush: false,
          baseDir: `${__dirname}`,
          debug: false,
          metadataProvider: JavaScriptMetadataProvider,

I'm sure that entities are in dir ./entities relatively to the module where initialization is performed.
Also, I specified metadataProvider: JavaScriptMetadataProvider (I'm not sure if it is needed really)
BTW, I can run locally without any problems.

Additional context
I'm using tsconfig-paths for modules resolving, can this cause the problem?

Versions
2.7.8

bug

All 3 comments

      entitiesDirs: ['/entities'], // this is absolute
      entitiesDirsTs: ['./entities'], // this is relative, and not even needed with JavaScriptMetadataProvider

I doubt that is what you want :]

Also every time you have some ORM related issues, enable debug mode (in v2 you will also need to manually provide logger, see https://mikro-orm.io/debugging/)

Then you should see what entities were discovered (probably none of them). In v3 there will be CLI command for configuration validation plus extended validation.

Is this about running your TS app compiled in production via node? It should work out of box with default MetadataProvider (TS one). Just ship your TS files too (they will be read via ts-morph), or provide all entity types manually.

See https://stackoverflow.com/questions/55523319/how-to-release-or-distribute-an-application-that-uses-mikro-orm

JavaScriptMetadataProvider is for usage with vanilla JS entities, and it requires you to export schema, which is the thing that is missing and the cause of your error.

https://mikro-orm.io/usage-with-js/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ml1nk picture ml1nk  路  6Comments

jsprw picture jsprw  路  6Comments

lookfirst picture lookfirst  路  4Comments

reramjiawan picture reramjiawan  路  6Comments

rubiin picture rubiin  路  7Comments