typeorm: typeorm-ts-node-commonjs doesnt work

Issue Description

I am trying to run the typeorm cli using typeorm-ts-node-commonjs to generate migrations and it doesn’t work.

Expected Behavior

It should work.

Actual Behavior

mgarcia:~/projects/compliance-backend-api(chore/add-typeorm)$ npm run typeorm

> compliance-backend-api@0.0.1 typeorm
> typeorm-ts-node-commonjs

/usr/bin/env: ‘node --require ts-node/register’: No such file or directory

Steps to Reproduce

  "dependencies": {
    "@appsignal/express": "^1.0.23",
    "@appsignal/nodejs": "^2.2.10",
    "cors": "^2.8.5",
    "cross-fetch": "^3.1.5",
    "dotenv": "^10.0.0",
    "envalid": "^7.2.1",
    "express": "^4.17.1",
    "express-async-handler": "^1.1.4",
    "http-terminator": "^3.0.4",
    "pg": "^8.7.3",
    "pino-http": "^5.8.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^5.5.12",
    "typeorm": "^0.3.4",
    "ws": "^3.3.3"
  },
  "devDependencies": {
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/jest": "^27.0.2",
    "@types/node": "^16.10.2",
    "@types/pino-http": "^5.8.0",
    "@types/supertest": "^2.0.11",
    "@types/ws": "^8.2.0",
    "@typescript-eslint/eslint-plugin": "^4.32.0",
    "@typescript-eslint/parser": "^4.32.0",
    "eslint": "^7.32.0",
    "jest": "^27.2.4",
    "jest-websocket-mock": "^2.2.1",
    "mock-socket": "^9.0.5",
    "nodemon": "^2.0.13",typeorm-ts-node-commonjs
    "supertest": "^6.1.6",
    "ts-jest": "^27.0.5",
    "ts-node": "^10.2.1",
    "tsc-watch": "^4.5.0",
    "typescript": "^4.4.3"
  },
  "engines": {
    "node": ">=16.10.0",
    "npm": ">=7.24.0"
  }
{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "ES6",
      "ES5"
    ],                                                /* Specify a set of bundled library declaration files that describe the target runtime environment. */
    "experimentalDecorators": true,                   /* Enable experimental support for TC39 stage 2 draft decorators. */
    "emitDecoratorMetadata": true,                    /* Emit design-type metadata for decorated declarations in source files. */
    "module": "commonjs",                                /* Specify what module code is generated. */
    "rootDir": "./src",                                  /* Specify the root folder within your source files. */
    "moduleResolution": "node",                          /* Specify how TypeScript looks up a file from a given module specifier. */
    "paths": {
      "*": [
        "./node_modules/*"
      ]
    },
    "sourceMap": true,                                   /* Create source map files for emitted JavaScript files. */
    "outDir": "./dist",                                   /* Specify an output folder for all emitted files. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "noImplicitAny": true,                               /* Enable error reporting for expressions and declarations with an implied `any` type.. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  },
  "exclude": [
    "spec/**/*.ts",
  ]
}

My Environment

Dependency Version
Operating System
Node.js version 16.10.0
Typescript version 4.4.3
TypeORM version 0.3.4

Additional Context

I used npx typeorm init in my current project.

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, but I don’t know how to start. I would need guidance.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 1
  • Comments: 21 (2 by maintainers)

Most upvoted comments

@marcel-mintouge i am done with typeorm for good(issues from 2019 are still present with a new look), I migrated to drizzle-orm 👌 so good luck to everyone !

I’ve found a solution for handling path aliases with typeorm-ts-node-commonjs (source).

You need to add the following to tsconfig.json:

{
  "ts-node": {
    "require": [
      "tsconfig-paths/register"
    ]
  },
  "compilerOptions": {
    ...
  }
}

Credit: #1556

I have the same issue. typeorm-ts-node-esm is broken in the same way.

@marcel-mintouge it is OK if it doesn’t bother you, but for me why should i switch to relative imports ? i prefer to avoid limited solutions. And i think that something similar to ts-node -r tsconfig-paths/register should be supported out of the box.

Migrations with typerom still a big mess !

If you are using typeorm-ts-node-esm or your goal is to run typeORM migrations with typescript and ESmodules(or Nodenext), and you are encountering problems with setting "type":"module" in package.json in your project, as a workaround, create your own migration scripts:

For example: src/scripts/run-migrations.ts

import { AppDataSource } from '../connection/appDataSource';

async function runMigrations() {
  try {
    await AppDataSource.initialize();

    await AppDataSource.runMigrations();

    console.log('Migrations ran successfully!');

    await AppDataSource.destroy();
  } catch (error) {
    console.error('Error running migrations:', error);
    process.exit(1);
  }
}

runMigrations();

Then run this file using ts-node:

"scripts": {
    "migration:run": "ts-node ./src/scripts/run-migrations.ts",
}

My tsconfig is this:

{
  "types": [ "node" ],
  "compilerOptions": {
    /* Language and Environment */
    "target": "es2016",
    /* Modules */
    "module": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    /* Type Checking */
    "strict": true,
    "skipLibCheck": true,
    "experimentalDecorators": true,
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

package.json:

{
  "name": "backend",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "ts-node src/index.ts",
    "dev": "nodemon src/index.ts",
    "lint": "eslint 'src/**/*.{js,ts}'",
    "format": "prettier --write .",
    "migration:run": "ts-node ./src/scripts/run-migrations.ts",
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "dotenv": "^16.4.5",
    "express": "^4.19.2",
    "pg": "^8.12.0",
    "typeorm": "^0.3.20"
  },
  "devDependencies": {
    "@types/express": "^4.17.21",
    "@types/node": "^22.5.0",
    "eslint": "^9.9.1",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-prettier": "^5.2.1",
    "nodemon": "^3.1.4",
    "prettier": "^3.3.3",
    "ts-node": "^10.9.2",
    "typescript": "^5.5.4",
    "typescript-eslint": "^8.2.0"
  }
}

Hope this helps somebody. I assume you could use this trick to resolve issues with typeorm-ts-node-commonjs as well, if you are using commonjs, make sure to replace imports in custom migration file wich commonjs requires.

@matiasgarcia I’m curious what your “typeorm” script looks like in your package.json…if you posted it, I’m not seeing it sorry 😉

I tried everything:

  1. “typeorm”: “typeorm-ts-node-commonjs”,
  2. “typeorm”: “typeorm”,
  3. “typeorm”: “typeorm-ts-node-esm”

My project compiles to commonjs so I guess the first option is fine but still.