NestJS, TypeScript and TypeORM error: Error during migration run

I have errors when I want to generate a migration from my API. Recently, I am trying to apply it. I could see that I need the ormconfig file in the root. After that, add the script commands in package.json to run, create, and generate the migrations. However, I can't generate or run them. The error I am having is the following:


> @punch-list-api/[email protected] migration:run
> npm run typeorm migration:run


> @punch-list-api/[email protected] typeorm
> typeorm-ts-node-commonjs -d ./ormconfig.ts migration:run

Error during migration run:
Error: Unable to open file: "path of the ormconfig". Cannot use import statement outside a module
    at Function.loadDataSource (/home/snuc/Documents/IOET_TT_Projects/punch-list-api/node_modules/src/commands/CommandUtils.ts:22:19)
    at async Object.handler (/home/snuc/Documents/IOET_TT_Projects/punch-list-api/node_modules/src/commands/MigrationRunCommand.ts:42:26)

I will share my configuration. ormconfig.ts:

import { DataSource } from 'typeorm';

const connectionSource = new DataSource({
  type: 'postgres',
  host: process.env.RDS_HOSTNAME,
  port: parseInt(process.env.RDS_PORT || '5432'),
  username: process.env.RDS_USERNAME,
  password: process.env.RDS_PASSWORD,
  database: process.env.RDS_DB_NAME,
  entities: [__dirname + '/dist/**/*.entity.js'],
  migrations: [__dirname + '/dist/database/migrations/*.js'],
});

export default connectionSource;

package.json scripts:

"typeorm": "typeorm-ts-node-commonjs -d ./ormconfig.ts",
"migration:create": "typeorm-ts-node-commonjs migration:create",
"migration:generate": "npm run typeorm migration:generate",
"migration:run": "npm run typeorm migration:run"

and just in case my tsconfig:

{
  "compilerOptions": {
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "lib": [
      "es2020",
      "dom"
    ],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {},
    "esModuleInterop": true,
    "resolveJsonModule": true
  },
  "files": [],
  "include": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ],
  "exclude": [
    "node_modules",
    "tmp"
  ]
}

I tried to use module.export but it gave me the error that a DataSource instance was required. I've tried some changes to the package.json scripts but they didn't help. I have only managed to create migrations but I have not been able to run them.

Upvotes: 1

Views: 143

Answers (1)

Enzear
Enzear

Reputation: 16

{

    "typeorm:cli": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -d dist/YOUR_CONFIG.js",
    "sync": "npm run build && npm run typeorm:cli --  schema:sync",
    "migration:revert": "npm run typeorm -- -d src/YOUR_CONFIG.ts migration:revert",
    "typeorm": "ts-node ./node_modules/typeorm/cli"
}
npm run sync

Upvotes: 0

Related Questions