Generating migrations with cli
Problem
Issue Description Expected Behavior Migrations get generated using the cli Actual Behavior I am following the documentation outlined here https://typeorm.io/migrations and I feel like the documentation is way different then what is actually happening in the cli. I have tried just running the following command: `typeorm migration:generate -n PostRefactoring` When I run this command I get the following: `Not enough non-option arguments: got 0, need at least 1` So then I modify the command to something like this: `Missing required argument: dataSource` So then from there I add the datasource since that is what it is asking for: `npx typeorm migration:generate test -d app-data-source.ts` (I found the -d somewhere online while investigating this issue) But once I run that command I get the following error: `File must contain a TypeScript / JavaScript code and export a DataSource instance.` I have been trying to get this to work for days and I am about ready to throw the towl in and use a different ORM...which I don't want to do. I know that everything is working fine because I can start up the app and the app connects to the database and creates rows in the database. So it is just something with the cli and trying to generate the migrations. Here is what is in my app-data-source.ts file: [code block] This is the contents of my app.ts file: [code block] All these files were setup using the instructions outlined in the express typeorm documentation: Example with
Error Output
error: `File must contain a TypeScript / JavaScript code and export a DataSource instance.`
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix TypeORM CLI Migration Generation Issue
The error occurs because the CLI command requires a valid DataSource instance to be exported from the specified file. The provided file may not be correctly exporting the DataSource, or the path to the file may be incorrect.
Awaiting Verification
Be the first to verify this fix
- 1
Verify DataSource Export
Ensure that your 'app-data-source.ts' file correctly exports a DataSource instance. The export should look like this:
typescriptimport { DataSource } from 'typeorm'; const AppDataSource = new DataSource({ type: 'postgres', host: 'localhost', port: 5432, username: 'user', password: 'password', database: 'database_name', entities: [/* your entities */], migrations: [/* your migrations */], }); export default AppDataSource; - 2
Update CLI Command
Modify your CLI command to correctly reference the exported DataSource. Use the following command format:
bashnpx typeorm migration:generate -n PostRefactoring -d ./app-data-source.ts - 3
Check File Path
Ensure that the path to 'app-data-source.ts' is correct relative to where you are running the command. If the file is in a different directory, adjust the path accordingly.
- 4
Confirm TypeScript Compilation
Make sure that your TypeScript files are being compiled correctly. If you are using ts-node, ensure it is installed and configured properly. You can run the following command to check for TypeScript compilation issues:
bashtsc --noEmit - 5
Test Migration Generation
After making the above changes, re-run the migration generation command to confirm that it works without errors.
bashnpx typeorm migration:generate -n PostRefactoring -d ./app-data-source.ts
Validation
To confirm the fix worked, check that the migration file is generated successfully in the migrations directory without any errors. Verify that the migration file contains the expected changes.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep