TypeORM CLI says it is missing the dataSource argument
Missing required argument: dataSourceShort answer
Since TypeORM 0.3 the CLI no longer reads ormconfig. Every command that touches the database needs the path to a file that creates your DataSource, passed with -d. If the file is TypeScript, run the CLI through typeorm-ts-node-commonjs or typeorm-ts-node-esm, and put -- before -d when you start it through an npm script.
Fix it fast
1Find out which cause you have
Run the command with the data source flag spelled out and look at the first line it prints. If it now connects, the flag was the only problem.
npx typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts2Match what you see, then jump to its fix
- 1
Missing required argument: dataSourceNo -d flag, because the project still relies on ormconfigFix - 2
Missing required argument: dataSource, although the script seems to have the flagThe -d flag is lost inside an npm scriptFix - 3
The CLI cannot load the .ts data source fileA TypeScript data source is started with the plain typeorm binaryFix
- 1
What does the error look like?
Missing required argument: dataSource
Cause 1 of 3
No -d flag, because the project still relies on ormconfig
1Check
TypeORM 0.3.0 dropped ormconfig for the CLI: the release notes say commands no longer support it and you must give a file with a data source instance. Check whether your scripts only call typeorm with a command name.
grep -n "typeorm" package.json
ls ormconfig* 2>/dev/null2Fix
Create one file that exports a DataSource, and pass its path with -d in every script that runs the CLI.
// src/data-source.ts
import { DataSource } from "typeorm"
export const AppDataSource = new DataSource({
type: "postgres",
url: process.env.DATABASE_URL,
entities: [__dirname + "/entities/**/*{.ts,.js}"],
migrations: [__dirname + "/migrations/**/*{.ts,.js}"],
})3Verify
Run the migrations through that file; the CLI connects and reports what it ran.
npx typeorm-ts-node-commonjs migration:run -d ./src/data-source.tsConfirmed by github.com
Cause 2 of 3
The -d flag is lost inside an npm script
1Check
The TypeORM docs write the run command with -- before -d, so that npm hands the flag to typeorm and does not keep it for itself. Look at how your script is written and how you call it.
grep -n "migration" package.json2Fix
Put the data source path in the script itself, or add -- when you call the script with extra flags.
{
"scripts": {
"typeorm": "typeorm-ts-node-commonjs",
"migration:run": "typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts"
}
}
npm run typeorm -- migration:run -d ./src/data-source.ts3Verify
Run the script and confirm it reaches the database.
npm run migration:runConfirmed by typeorm.io
Cause 3 of 3
A TypeScript data source is started with the plain typeorm binary
1Check
The plain typeorm command loads JavaScript. The docs give two wrappers for TypeScript files: one for CommonJS projects and one for ESM projects. Check which module type your project uses.
grep -n '"type"' package.json2Fix
Use typeorm-ts-node-commonjs when package.json has no "type": "module", and typeorm-ts-node-esm when it does.
npx typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts
# ESM project:
npx typeorm-ts-node-esm migration:run -d ./src/data-source.ts3Verify
The command connects and runs the pending migrations.
npx typeorm-ts-node-commonjs migration:run -d ./src/data-source.tsConfirmed by typeorm.io
How do I stop it coming back?
- Keep one data-source file and import its options from the app, so the app and the CLI cannot drift apart.
- Wrap every CLI call in an npm script that already contains -d.
- Read the data source options from environment variables, not from a config file that only the app loads.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
NestJS app will not start: Cannot find module '/app/dist/main'
Cannot find module 'dist/main'
Node cannot find dist/main.js because the build did not put it there.
4 causes, each with a fixBack end
TypeError: something.default is not a constructor in a NestJS app
Exception has occurred: TypeError: xxx.default is not a constructor
TypeScript turns import X from 'pkg' into require('pkg').default when esModuleInterop is off, and a CommonJS package has no default, so new X() fails.
2 causes, each with a fixBack end
TypeScript cannot find req.cookies in a NestJS controller
error TS2339: Property 'cookies' does not exist on type
TypeScript says 'cookies' is missing because the Request type in your file is not Express's, or because cookie support is not installed.
3 causes, each with a fixBack end
TypeORM warns that createConnection is deprecated
'createConnection' is deprecated.
TypeORM replaced Connection with DataSource.
2 causes, each with a fixBack end
TypeORM stops with Duplicate migrations on run or generate
Error: Duplicate migrations: User1655123435373
TypeORM collects every migration class that your migrations glob matches and refuses to continue when two of them have the same class name.
2 causes, each with a fix