Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM cannot use cli with configuration

I have my (typescript) Project (Its a discord bot if that is important), which contains a "ormconfig.json" file. This file contains my configuration for TypeORM, see as follows:

{
    "type": "mysql",
    "host": "<IP of my server>",
    "port": 3306,
    "username": "<MySQL User>",
    "password": "<MySQL Password>",
    "database": "<existing MySQL database>",
    "synchronize": true,
    "logging": false,
    "entities": [
        "src/entities/*.ts"
    ],
    "migrations": [
        "src/migration/*.ts"
    ],
    "subscribers": [
        "src/subscriber/*.ts"
    ],
    "cli": {
        "entitiesDir": "src/entities/",
        "migrationsDir": "src/migration/",
        "subscribersDir": "src/subscriber/"
    }
}

Now, if I run anything on my actual server, the configuration in general works. Entities get loaded correctly and I can use them as intended. Now I made some changes in an entity, which means I would have to do a migration. Trying to generate the migration made the actual problem appear.

I tried running the following command:

npm run typeorm migration:show -- -d .\ormconfig.json

I expected this to load the config, connect to the server, check the database and give me output. Yet when it loads, it simply throws an error, saying

Given data source file must contain export of a DataSource instance

I also tried doing it over javascript, yet when I export a "DataSource"-Instance, i receive a similar error:

Invalid file path given: "C:\temp\Bot\src\database.ts". File must contain a TypeScript / JavaScript code and export a DataSource instance.

Following also my package.json file, for the dependencies of the project.

{
  "name": "Bot",
  "version": "1.1.1",
  "description": "desc",
  "main": "index.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "serve": "npx ts-node src/index.ts",
    "start": "npx ts-node src/index.ts",
    "typeorm": "npx --require ts-node/register ./node_modules/typeorm/cli.js",
  },
  "author": "PlayWolfYT",
  "license": "ISC",
  "dependencies": {
    "@discordjs/builders": "^0.12.0",
    "@discordjs/rest": "^0.3.0",
    "@types/nodemailer": "^6.4.4",
    "dayjs": "^1.10.8",
    "discord-api-types": "^0.27.0",
    "discord.js": "^13.6.0",
    "mysql2": "^2.3.3",
    "nodemailer": "^6.7.2",
    "nodemon": "2.0.15",
    "reflect-metadata": "^0.1.13",
    "ts-node": "^10.5.0",
    "typeorm": "^0.3.3",
    "typescript": "^4.6.2"
  },
  "devDependencies": {
    "@types/node": "^17.0.21",
    "@discordjs/builders": "^0.12.0",
    "@discordjs/rest": "^0.3.0"
  }
}
like image 775
PlayWolfYT Avatar asked Mar 29 '26 02:03

PlayWolfYT


1 Answers

So, you are using one of the newer versions of Typeorm, in which they dropped support to ormconfig.{json,xml} file. You'll have to put your database connection options in a {ts,js} file using a Data Source and export it.

Two options to work with your problem:

  • Downgrade your typeorm to 0.2.45: npm i [email protected];
  • Update your code following the new docs: Typeorm docs;

I can help you setup through editing this answer if needed, just comment it below.

like image 183
Luis Vinicius M. Avatar answered Mar 31 '26 03:03

Luis Vinicius M.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!