Mikro-orm: Error: Source file for entity X not found check your 'entitiesDirsTs' option. If you are using webpack, ...

Created on 6 May 2020  路  4Comments  路  Source: mikro-orm/mikro-orm

Describe the bug
A clear and concise description of what the bug is.

Disclaimer: This is probably my fault and not necessarily a bug.
I have been following the Deployment Guide on the Documentation and I have setup a project where I have an entity folder and I have all my entities there and I have referenced them via both entitiesDirs and entitiesDirsTs. Now, either if I generate the production ready Cache via mikro-orm cache:generate and copy the source files in the build directory to be available both at the root and the build folder or if I don't generate the cache and use the development cache, the result is the same and I get the error in the Stack trace section below.

Stack trace

UnhandledPromiseRejectionWarning: Error: Source file for entity /BaseEntity.ts not found, check your 'entitiesDirsTs' option. If you are using webpack, see https://bit.ly/35pPDNn
    at TsMorphMetadataProvider.getSourceFile (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/metadata/TsMorphMetadataProvider.js:75:19)
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
    at async TsMorphMetadataProvider.readTypeFromSource (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/metadata/TsMorphMetadataProvider.js:54:24)
    at async TsMorphMetadataProvider.initPropertyType (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/metadata/TsMorphMetadataProvider.js:45:36)
    at async TsMorphMetadataProvider.initProperties (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/metadata/TsMorphMetadataProvider.js:30:17)
    at async TsMorphMetadataProvider.loadEntityMetadata (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/metadata/TsMorphMetadataProvider.js:19:9)
    at async MetadataDiscovery.discoverEntity (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/metadata/MetadataDiscovery.js:131:13)
    at async MetadataDiscovery.discoverDirectory (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/metadata/MetadataDiscovery.js:92:13)
    at async Function.runSerial (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/utils/Utils.js:395:22)
    at async MetadataDiscovery.findEntities (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/metadata/MetadataDiscovery.js:66:13)
    at async MetadataDiscovery.discover (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/metadata/MetadataDiscovery.js:31:9)
    at async Function.init (/Users/ali/Desktop/ObjectObject/ObjObjMikro/node_modules/mikro-orm/dist/MikroORM.js:45:24)
(node:28055) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:28055) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
...

To Reproduce
Steps to reproduce the behavior:

  1. Create the mikro-orm.config.ts file as it is mentioned in the Additional Context section Below.
  2. Build the Project via "tsc"
  3. Copy the temp cache folder to the ./build folder
  4. run node ./build/src/index.js
  5. The error shows up

Expected behavior
A clear and concise description of what you expected to happen.

The compiled server should start without any error and it is notable that the server is working normally with ts-node in development and this error is probably caused by a caching issue.

Additional context
Add any other context about the problem here.

For the Context I have declared the following in my mikro-orm.config.ts

import { Options } from "mikro-orm";

const config = {
  type: "sqlite",
  dbName: "obj.sqlite",
  entitiesDirs: ["./src/entity/**"],
  entitiesDirsTs: ["./src/entity/**"],
  discovery: {
    tsConfigPath: "./tsconfig.json",
  },
  debug: true,
  baseDir: __dirname,
  autoFlush: true,
} as Options;

export default config;

We also wanted to try the ReflectMetadataProvider but because of the limitations of Reflect Meta Data that we had to rewrite a massive portions of our code which was a conscious decision to stick with ts-morph.

Other Additional Context:

tsconfig.json:

{
  "compilerOptions": {
    "lib": ["es2016", "esnext.asynciterable"],
    "target": "es2016",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./build",
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "resolveJsonModule": true,
    "allowJs": true
  }
}

Versions

| Dependency | Version |
| - | - |
| node |12.9.1 |
| typescript | 3.8.3 |
| mikro-orm | 3.6.13|
| sqlite3 | 4.2.0 |

question

Most helpful comment

This is wrong:

entitiesDirs: ["./src/entity/**"],
entitiesDirsTs: ["./src/entity/**"],
  • entitiesDirs - this needs to point to your compiled entities (*.js)
  • entitiesDirsTs - this needs to point to your source entities (*.ts)

When you use ts-node, it is automatically detected and the entitiesDirsTs is used instead. When you use plain node, we use the entitiesDirsTs only for ts-morph discovery.

So based on your tsconfig.json, I guess you want entitiesDirs: ["./build/entity/**"] or something like that (maybe ./src/build).

All 4 comments

This is wrong:

entitiesDirs: ["./src/entity/**"],
entitiesDirsTs: ["./src/entity/**"],
  • entitiesDirs - this needs to point to your compiled entities (*.js)
  • entitiesDirsTs - this needs to point to your source entities (*.ts)

When you use ts-node, it is automatically detected and the entitiesDirsTs is used instead. When you use plain node, we use the entitiesDirsTs only for ts-morph discovery.

So based on your tsconfig.json, I guess you want entitiesDirs: ["./build/entity/**"] or something like that (maybe ./src/build).

That is correct! I just successfully built and ran the server, thanks a lot! And here's my final mikro-orm.config.ts that I got working:


import { Options } from "mikro-orm";
const config = {
  type: "sqlite", 
  dbName: "obj.sqlite",
  entitiesDirs: ["./build/src/entity/**"], // This one, as you said, was one of the major issues.
  entitiesDirsTs: ["./src/entity/**"],
  discovery: {
    tsConfigPath: "./tsconfig.json",
    warnWhenNoEntities: true,
  },
  debug: true,
  // baseDir: __dirname, => This one was also an issue because it could not resolve modules in the src folder and we want it to be the default `process.cwd()` instead.
  autoFlush: true,
} as Options;

export default config;

Many Thanks!
Feel free to close the issue now.

Just fyi, the warnWhenNoEntities is there for internal usage, it defaults to true, and is set to false only when using CLI, particularly only when using commands that make sense to have no entities discovered (e.g. entity generator).

Interesting! I just removed it. Thanks a lot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iDavidB picture iDavidB  路  4Comments

gustavopch picture gustavopch  路  4Comments

Langstra picture Langstra  路  6Comments

rubiin picture rubiin  路  7Comments

jsprw picture jsprw  路  6Comments