FG
๐Ÿ—„๏ธ Databases

Migration should either read .ts or deliver a .js

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score59%
59%

Problem

This is either a bug or a new feature [ ] question [ x] bug report [x ] feature request [ ] documentation issue Database system/driver: [ ] `cordova` [ ] `mongodb` [ ] `mssql` [ ] `mysql` / `mariadb` [ ] `oracle` [ x] `postgres` [ ] `sqlite` [ ] `sqljs` [ ] `websql` TypeORM version: [x ] `latest` [ ] `@next` [ ] `0.x.x` (or put your version here) Steps to reproduce or a small repository showing the problem: `typeorm migrations:generate -n test` this create typecript file test.ts `typeorm migrations:run` this fails with `Unexpected token import` What solves the issue Either call typeorm using ts-node, as explained here : https://github.com/typeorm/typeorm/issues/1561 Either transpile .ts file .js using typescript Expected behaviour Typeorm should be able to read the file it generated, which means : reading ts file or providing .js file

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Enable TypeORM to Handle TypeScript Migrations

Medium Risk

TypeORM generates migration files in TypeScript (.ts) format, but when executing migrations, it attempts to run them as JavaScript (.js) files. This results in an 'Unexpected token import' error because Node.js does not natively support ES module syntax in .ts files without a transpiler like ts-node or a pre-transpilation step.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Install ts-node

    Install ts-node globally or as a dev dependency in your project to allow TypeScript files to be executed directly.

    bash
    npm install -g ts-node
  2. 2

    Run TypeORM Migrations with ts-node

    Use ts-node to run TypeORM migrations, allowing TypeORM to execute TypeScript files directly.

    bash
    ts-node ./node_modules/typeorm/cli.js migration:run
  3. 3

    Transpile TypeScript Migrations to JavaScript

    Alternatively, transpile the generated TypeScript migration files to JavaScript using the TypeScript compiler (tsc). Ensure your tsconfig.json is properly configured.

    bash
    tsc --outDir ./dist ./migrations/*.ts
  4. 4

    Run Transpiled Migrations

    After transpiling, run the migrations from the output directory where the .js files are located.

    bash
    typeorm migration:run --dir ./dist/migrations

Validation

Confirm the fix by running the migration command again. If the migrations execute successfully without errors, the issue is resolved.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

typeormormtypescriptinvalid