Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeorm-ts-node-commonjs doesnt work #8824

Open
matiasgarcia opened this issue Mar 29, 2022 · 16 comments
Open

typeorm-ts-node-commonjs doesnt work #8824

matiasgarcia opened this issue Mar 29, 2022 · 16 comments

Comments

@matiasgarcia
Copy link

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.
@matiasgarcia
Copy link
Author

Also, the scaffold generated an Entity:

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"

@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number

    @Column()
    firstName: string

    @Column()
    lastName: string

    @Column()
    age: number

}

And Typescript doesnt seem to recognize typeorm types.

Could not find a declaration file for module 'typeorm'. '/home/mgarcia/projects/compliance-backend-api/node_modules/typeorm/index.js' implicitly has an 'any' type.
Try npm i --save-dev @types/typeorm if it exists or add a new declaration (.d.ts) file containing declare module 'typeorm';

@codeninja
Copy link

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

@pleerock
Copy link
Member

adding @giladgd

@msalsbery
Copy link

@matiasgarcia I'm curious what your "typeorm" script looks like in your package.json...if you posted it, I'm not seeing it sorry ;)

@giladgd
Copy link
Contributor

giladgd commented Mar 29, 2022

PR #8821 fixes this, once a new version is released this should be solved

@matiasgarcia
Copy link
Author

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

@matiasgarcia
Copy link
Author

@matiasgarcia
Copy link
Author

PR #8821 fixes this, once a new version is released this should be solved

Is there any workaround for this in the meantime?

@msalsbery
Copy link

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

See #8810 After researching a few issues here I got it working

@akivajgordon
Copy link

I was on typeorm v0.3.4 with the same issue.

I upgraded to v0.3.6 and the problem went away, thanks to #8821 (fixed in 0.3.5 actually).

@odusseys
Copy link

odusseys commented Jun 5, 2023

no luck for me, tried almost everything ... ESM support is terrible

@ItsTarik
Copy link

Migrations with typerom still a big mess !

@marcel-mintouge
Copy link

@ItsTarik Were you able to fix that?

@ItsTarik
Copy link

@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 !

@marcel-mintouge
Copy link

It worked for me in the end with typeorm-ts-node-esm, I just had some absolute imports that weren't working well with the script, so I made them relative.

typeorm-ts-node-esm migration:generate -d <path-to-datasource> ...

@ItsTarik
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants