Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM error when trying to create migration in CLI

I'm trying to use the TypeORM CLI to create a migration. I followed their guide but when I run yarn run typeorm migration:generate I get the error:

$ typeorm-ts-node-commonjs migration:generate
/usr/bin/env: ‘node --require ts-node/register’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines
error Command failed with exit code 127.

My package.json looks like this:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "tsc -w",
    "start": "node dist/index.js",
    "dev": "nodemon dist/index.js",
    "start2": "ts-node src/index.ts",
    "dev2": "nodemon --exec ts-node src/index.ts",
    "typeorm": "typeorm-ts-node-commonjs"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "apollo-server-express": "^3.6.7",
    "class-validator": "^0.13.2",
    "cors": "^2.8.5",
    "express": "^4.17.3",
    "express-graphql": "^0.12.0",
    "graphql": "^15.3.0",
    "pg": "^8.7.3",
    "reflect-metadata": "^0.1.13",
    "ts-node": "^10.7.0",
    "type-graphql": "^1.1.1",
    "typeorm": "^0.3.4"
  },
  "devDependencies": {
    "@types/express": "^4.17.13",
    "@types/node": "^17.0.23",
    "nodemon": "^2.0.15",
    "typescript": "^4.6.3"
  }
}
like image 626
sev Avatar asked Sep 29 '26 16:09

sev


1 Answers

I managed to solve this by installing the cli as described here https://orkhan.gitbook.io/typeorm/docs/using-cli#installing-cli

Install ts-node globally npm install -g ts-node

add the following script to package.json: "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js" instead of "typeorm": "typeorm-ts-node-commonjs"

then create a migration with: yarn run typeorm migration:generate -p -d src/data-source.ts src/migrations/<filename without ending>

FYI For anyone facing similar issues: It took me a while to figure out that the various documentations on typeorm are confusing and conflicting at the moment because they just moved to v0.3.0 and not everything has been updated.

Docs

https://typeorm.io/docs/getting-started/

https://github.com/typeorm/typeorm

like image 200
sev Avatar answered Oct 02 '26 06:10

sev