Every time I try to execute the app.ts file, I encounter the EntityMetadataNotFoundError which states that there is no metadata available for the entity "user"

I am currently working on a project using ExpressJS, TypeORM, and TypeScript. However, I have encountered an issue when running the project in develop mode (with app.ts). The first request triggers the following error:

node_modules/src/data-source/DataSource.ts:427
        if (!metadata) throw new EntityMetadataNotFoundError(target)
                             ^
EntityMetadataNotFoundError: No metadata for "user" was found.

Strangely, everything works fine when I start the project from /dist/app.js.

I have set es2020 as the target, nodeNext as the module, and specified the moduleResolution in tsconfig. Here is a snippet of my package.json file: `

 "type": "module",
  "scripts": {
    "start": "tsc -p . && node dist/app.js",
    "start:prod": "node dist/app.js",
    "dev": "ts-node-esm app.ts --watch",
    "test": "mocha -r ts-node/register src/**/*.ts",
    "test-dev": "nodemon --watch . --ext ts --exec \"mocha -r ts-node/register src/**/*.ts\"",
    "build": "rimraf dist && tsc -p .",
}

`

In addition, here is the content of my typeorm.config.ts file:

`

config();
const baseDir = 'dist/src';

export const appDataSource = new DataSource({
  type: 'postgres',
  host: process.env.POSTGRES_DB_HOST,
  port: Number(process.env.POSTGRES_DB_PORT),
  username: process.env.POSTGRES_DB_USER,
  password: process.env.POSTGRES_DB_PASSWORD,
  database: process.env.POSTGRES_DB_DATABASE,
  synchronize: false,
  logging: true,
  entities: [baseDir + '/**/entity/*.entity.js'],
  migrationsRun: true,
  migrations: [baseDir + '/migrations/*.js'],
});

`

I have attempted to include .ts extensions in the entities and migrations paths but the issue persists.

Answer №1

When transitioning to production mode, I encountered an issue that required adding "type": "module" to my package.json file and making additional changes to my tsconfig file. However, this led to errors in development mode due to .ts extensions not being recognized in my typeorm.config file. To resolve this, I had to specify separate paths for entities and migrations in the typeorm.config file.

config();
const entitiesString =
  process.env.NODE_ENV == 'production'
    ? 'dist/src/**/entity/*.entity.js'
    : 'src/**/entity/*.entity.ts';

const migrationString =
  process.env.NODE_ENV == 'production'
    ? 'dist/src/migrations/*.js'
    : 'src//migrations/*.ts';

export const appDataSource = new DataSource({
  type: 'postgres',
  host: process.env.POSTGRES_DB_HOST,
  port: Number(process.env.POSTGRES_DB_PORT),
  username: process.env.POSTGRES_DB_USER,
  password: process.env.POSTGRES_DB_PASSWORD,
  database: process.env.POSTGRES_DB_DATABASE,
  synchronize: false,
  logging: true,
  entities: [entitiesString],
  migrationsRun: true,
  migrations: [migrationString],
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting issue with absolute paths in Vite project using React and TypeScript

I'm having trouble implementing absolute paths in a Vite react-ts project. This is how I set up the project: npm init @vitejs/app npx: installed 6 in 1.883s √ Project name: ... test-vite √ Select a framework: » react √ Select a variant: » rea ...

What is the best way to send out Redux actions?

I'm in the process of creating a demo app with authorization, utilizing redux and typescript. Although the action "loginUser" in actions.tsx is functioning, the reducer is not executing as expected. Feel free to take a look at my code below: https:/ ...

Tips for working with dates in ReactJS

Currently, I am retrieving my backend date in the following format: 2020-01-01T03:00:00.000Z For my project, I am utilizing the daterangepicker component from the AdminLTE template. Here is an example of my field implementation: <div className=" ...

Application utilizing PugJS for node interaction

Recently delving into the world of Node.js, I managed to construct an application that retrieves user data (specifically the creation date) and provides insights on daily, weekly, monthly, and yearly users. The functionality was thoroughly tested using Pos ...

Firestore CollectionReference and DocumentReference types are lacking essential methods like .add(), .set(), .update(), etc

I am currently utilizing Firestore alongside React and Typescript. My goal is to fetch data from Firestore and update it. Despite finding numerous examples online with syntax involving methods, my IDE does not recognize these methods for the types involved ...

"Building with Node.js and Express: A Comprehensive Boilerplate for Efficient

Experimenting with Node.js and the Express framework using the Express boilerplate installation has been quite a journey. It wasn't immediately clear that Redis needed to be installed (perhaps a note in the boilerplate would have helped) or how to nav ...

Guide to importing a class property from one file to another - Using Vue with Typescript

Here is the code from the src/middlewares/auth.ts file: import { Vue } from 'vue-property-decorator' export default class AuthGuard extends Vue { public guest(to: any, from: any, next: any): void { if (this.$store.state.authenticated) { ...

The response data from Axios cannot be stored using useState hook

Struggling with fetching data from my express backend and MySQL database to display on my react frontend using axios. However, I'm facing issues when trying to update the fetched data using the useState hook. Here is how my frontend function is struc ...

Typescript - Creating a generic type that extends its own type signature

Recently, I stumbled upon the following code snippet: interface Test<T extends Test<T>> { a: number; b: T; } function foo <T extends Test<T>>(el: T): T { ... } I must admit, I am a bit perplexed by this and wondering about ...

Tips for setting up karma to allow for the debuggability of TypeScript source files

I recently downloaded an Angular2 Webpack Starter seed project from GitHub and managed to set it up without encountering any issues. However, I have run into a slight inconvenience when trying to debug source files under unit tests. All *.spec.ts files are ...

The ranking of type parameter inference in TypeScript applications

I've created a unique decorator for classes that accepts an initializer function as its argument. Inside this `initializer` function, I want to be able to return an instance that matches the class type or a derived one: function JsonObject<T>(i ...

The error message from Sqlite3 is being displayed on a Postgresql application

Currently in the process of learning how to construct an app utilizing node express and knexjs to establish a connection with the Postgresql database. Encounter an issue with a warning message appearing when attempting a post request, indicating that the d ...

Tips on preventing the duplication of component instances in your project

Check out the plunker link to see that the child component "Loader" is being loaded multiple times every time the button is clicked. How can I prevent creating multiple instances of the same component? I want the new instance to replace the existing one wh ...

Error: Loki cannot be used as a constructor

Can anyone assist me in understanding why this code is not functioning correctly? Here's what my index.ts file in Hapi.js looks like: import { Server, Request, ResponseToolkit } from '@hapi/hapi'; import * as Loki from 'lokijs'; ...

Listening for changes in Firestore from a Node.js Express server in order to send multiple responses

I am working with React.js to make requests to the server and receive an array of data, updating for each change in the collection. On the server side, I am using Node.js with Express and the Admin SDK to fetch data from Firebase. Below is the code snippet ...

Passport/Express: Issue with User Authentication following registration of New User

I currently have a NodeJS/Express server up and running with PassportJS for authentication. Successfully implemented login functionality for existing users, but encountering issues with authenticating new users. Even though the user is created successfull ...

Difficulty encountered in a Cast when using Router.put with mongoDB in a MERN application

Currently, I am trying to implement the router.put method for updating the Boolean value (isOn) in a toggle button. However, before making any changes, I decided to test it out and now I am encountering an issue. const express = require("express"); const ...

How can MongooseJS be utilized to correctly push the desired format?

I am facing an issue with creating data in a specific format: { "weather": { "dailysummary": [...], "created_on": "2016-10-27 11:11:11", "addedOnLastHour": false } } However, I keep getting a different format from Mongoose ...

Error encountered in Openshift with haproxy: 'express' does not have any server accessible

After setting up a basic Nodejs app on Openshift, I noticed that the service would randomly terminate itself every few days. Strangely, there were no error messages from my Nodejs module, but instead from haproxy, which was automatically installed along wi ...

Retrieving information from an Express.js API using React.js. Postman requests are successfully communicating with the API

Hey there, I'm facing a little issue and could use some help. I have an Express application that is working perfectly with requests from Postman (adding, deleting, etc.). Now, I want to connect my client side (React.js) with the existing API using the ...