TypeORM CommonJS TypeScript CLI Executor

0.3.20 · maintenance · verified Sun Apr 19

The `typeorm-ts-node-commonjs` package provides a command-line interface (CLI) wrapper specifically designed to execute TypeORM commands in TypeScript projects utilizing the CommonJS module system. It acts as an intermediary, configuring `ts-node/register` with appropriate settings to allow the TypeORM CLI (for tasks like migrations, schema synchronization, or entity generation) to correctly process TypeScript source files within a CommonJS environment. This package addresses compatibility challenges that historically arose when TypeORM's CLI, which typically requires `ts-node` for TypeScript compilation, encountered CommonJS module resolution. Although not directly maintained by the core TypeORM team, its usage is officially recommended in TypeORM's documentation for CommonJS TypeScript setups. The current stable version is 0.3.20, with the last update over a year ago, suggesting an infrequent release cadence driven by specific compatibility needs rather than active feature development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `package.json` scripts to use `typeorm-ts-node-commonjs` for TypeORM CLI commands in a TypeScript project configured for CommonJS modules, including a basic DataSource setup.

{
  "name": "my-typeorm-cjs-app",
  "version": "1.0.0",
  "main": "index.js",
  "type": "commonjs",
  "scripts": {
    "start": "node src/index.js",
    "typeorm": "typeorm-ts-node-commonjs",
    "typeorm:migration:generate": "npm run typeorm -- migration:generate -d ./src/data-source.ts ./src/migrations/MyNewMigration",
    "typeorm:migration:run": "npm run typeorm -- migration:run -d ./src/data-source.ts",
    "typeorm:schema:sync": "npm run typeorm -- schema:sync -d ./src/data-source.ts"
  },
  "dependencies": {
    "typeorm": "^0.3.20",
    "reflect-metadata": "^0.1.13",
    "pg": "^8.11.3"
  },
  "devDependencies": {
    "ts-node": "^10.9.1",
    "typescript": "^5.3.3",
    "typeorm-ts-node-commonjs": "^0.3.20"
  }
}
// src/data-source.ts
import "reflect-metadata";
import { DataSource } from "typeorm";
import { User } from "./entity/User";

export const AppDataSource = new DataSource({
    type: "postgres",
    host: process.env.DB_HOST ?? "localhost",
    port: parseInt(process.env.DB_PORT ?? "5432", 10),
    username: process.env.DB_USERNAME ?? "user",
    password: process.env.DB_PASSWORD ?? "password",
    database: process.env.DB_NAME ?? "mydatabase",
    synchronize: false,
    logging: false,
    entities: [User],
    migrations: [__dirname + "/migrations/**/*.ts"],
    subscribers: [],
});

// To run, assuming package.json is set up as above:
// npm install
// npm run typeorm:migration:generate

view raw JSON →