Search code examples
javascripttypescriptexpresstypeormtsc

Typeorm & Typescript EntityMetadataNotFoundError On Compiled Javascript Files


i'm trying to build a backend with nodejs & express & typeorm using typescript. I have a problem that when i run application with nodemon it's work like intended. But when i start with node app.js on compiled javascript files and send request to any end point it's givin me the error EntityMetadataNotFoundError. How can i get pass this issue ?

ormconfig.json

{
    "type": "postgres",
    "host": "localhost",
    "port": 5432,
    "username": "",
    "password": "",
    "database": "",
    "entities": [
        "dist/src/entity/**/*.js",
        "src/entity/**/*.ts"
    ],
    "migrations": [
        "dist/src/migration/**/*.js",
        "src/migration/**/*.ts"
    ],
    "subscribers": [
        "dist/src/subscriber/**/*.js",
        "src/subscriber/**/*.ts"
    ],
    "cli": {
        "entitiesDir": "dist/src/entity",
        "migrationsDir": "dist/src/migration",
        "subscribersDir": "dist/src/subscriber"
    }
}

i don't know if needed but here is tsconfig.json

{
   "compilerOptions": {
      "lib": [
         "es5",
         "es6",
         "es7",
         "es2015",
         "es2016",
         "es2017",
         "es2018",
         "esnext"
      ],
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "Node",
      "outDir": "dist",
      "sourceMap": true,
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true
   }
}

my build script is tsc --project .

Any help is appreciated, thank you.


Solution

  • Okay,i figured out. tsc --project . command not copying the ormconfig.json file to dist directory. I changed tsconfig.json and it works now!

    I added include and resolveJsonModule properties.

    tsconfig.json

    {
       "compilerOptions": {
          "lib": [
             "es5",
             "es6",
             "es7",
             "es2015",
             "es2016",
             "es2017",
             "es2018",
             "esnext"
          ],
          "target": "es5",
          "module": "commonjs",
          "moduleResolution": "Node",
          "resolveJsonModule": true,
          "outDir": "dist",
          "sourceMap": true,
          "experimentalDecorators": true,
          "emitDecoratorMetadata": true
       },
       "include": [
          "ormconfig.json"
       ]
    }