Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeORM CLI says it is missing the dataSource argument

ErrorMissing required argument: dataSource

Short 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.

  • Applies to TypeORM 0.3.x, Node.js 22, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.ts
  2. 2Match what you see, then jump to its fix

    1. 1Missing required argument: dataSourceNo -d flag, because the project still relies on ormconfigFix
    2. 2Missing required argument: dataSource, although the script seems to have the flagThe -d flag is lost inside an npm scriptFix
    3. 3The CLI cannot load the .ts data source fileA TypeScript data source is started with the plain typeorm binaryFix

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/null

2Fix

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.ts

Confirmed by github.com

Did this fix it?

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.json

2Fix

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.ts

3Verify

Run the script and confirm it reaches the database.

npm run migration:run

Confirmed by typeorm.io

Did this fix it?

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.json

2Fix

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.ts

3Verify

The command connects and runs the pending migrations.

npx typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts

Confirmed by typeorm.io

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.