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

Can not generate migrations with capacitor driver: "Cannot use import statement outside a module"d #10881

Open
1 of 18 tasks
elpollodiablo opened this issue May 7, 2024 · 1 comment

Comments

@elpollodiablo
Copy link

elpollodiablo commented May 7, 2024

Issue description

When using the capacitor driver and a typescript datasource, I can not successfully run migration:generate

Expected Behavior

Create a migration file

Actual Behavior

 % npx typeorm-ts-node-commonjs migration:generate -d src/app/data-sources/AuthorDataSource.ts foo
Error during migration generation:
Error: Unable to open file: "/Users/philip/Cloud/workspace/ionic-sqlite-typeorm-app/src/app/data-sources/AuthorDataSource.ts". Cannot use import statement outside a module
    at Function.loadDataSource (/Users/philip/Cloud/workspace/ionic-sqlite-typeorm-app/node_modules/src/commands/CommandUtils.ts:22:19)
    at async Object.handler (/Users/philip/Cloud/workspace/ionic-sqlite-typeorm-app/node_modules/src/commands/MigrationGenerateCommand.ts:78:26)
philip@MacBook-Pro ~/workspace/ionic-sqlite-typeorm-app
 % npx typeorm-ts-node-esm migration:generate -d src/app/data-sources/AuthorDataSource.ts foo
Error during migration generation:
Error: Unable to open file: "/Users/philip/Cloud/workspace/ionic-sqlite-typeorm-app/src/app/data-sources/AuthorDataSource.ts". Cannot use import statement outside a module
    at Function.loadDataSource (/Users/philip/Cloud/workspace/ionic-sqlite-typeorm-app/node_modules/src/commands/CommandUtils.ts:22:19)
    at async Object.handler (/Users/philip/Cloud/workspace/ionic-sqlite-typeorm-app/node_modules/src/commands/MigrationGenerateCommand.ts:78:26)

Steps to reproduce

Check out and set up https://github.com/jepiqueau/ionic-sqlite-typeorm-app then run typeorm migration:generate -d src/app/data-sources/AuthorDataSource.ts foo

My Environment

Dependency Version
Operating System OSX Sonoma 14.1.1
Node.js version v18.17.1
Typescript version 5.2.2
TypeORM version 0.3.20

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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.

@nkfrv1
Copy link

nkfrv1 commented May 12, 2024

I am experiencing the same problem with the default postgres driver. So, I don't think this kind of error is tied to driver stuff.

However, there are two workarounds known to me:

  • use npx ts-node -r tsconfig-paths/register node_modules/typeorm/cli -d <path/to/datasource> path/to/migrations/<migration-name> to generate a migration file based on changes to TypeScript entities

    Old, but still relevant reply from another issue

  • use npx typeorm-ts-node-commonjs migration:generate -d <path/to/datasource> path/to/migrations/<migration-name>

    With this option, it is necessary to specify path to the JavaScript entities from the dist directory
    (e.g. entities: [path.resolve('dist', '**', '*.entity.js')])

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

2 participants