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't generate migrations in test project #9760

Open
1 of 18 tasks
sharikovvladislav opened this issue Feb 6, 2023 · 4 comments
Open
1 of 18 tasks

Can't generate migrations in test project #9760

sharikovvladislav opened this issue Feb 6, 2023 · 4 comments

Comments

@sharikovvladislav
Copy link

sharikovvladislav commented Feb 6, 2023

Issue description

I have created test project with a playground, like the docs states. After that I am trying to generate migrations, so I can run them on a server later, I am getting the error during migrations generation.

Expected Behavior

  • a migration file is created
  • generate command works without datasource specified / or docs should say that I have to specify a datasource

Actual Behavior

An error happens during the CLI command execution.

Steps to reproduce

Here is the console output. Init -> generate -> generate with a datasource specified

[~/tmp]$ npx typeorm init --name MyProject --database postgres                                                                                                                                                                                    
npx: installed 64 in 6.586s
Project created inside /Users/vladsharikov/tmp/MyProject directory.
Please wait, installing dependencies...
Done! Start playing with a new project!
[~/tmp]$ cd MyProject
[~/tmp/MyProject]$ npx typeorm migration:generate -n test                                                                                                                                                                                         rvm:ruby-2.4.10
typeorm migration:generate <path>

Generates a new migration file with sql needs to be executed to update schema.

Options:
  -h, --help          Show help                                        [boolean]
  -d, --dataSource    Path to the file where your DataSource instance is
                      defined.                               [string] [required]
  -p, --pretty        Pretty-print generated SQL      [boolean] [default: false]
  -o, --outputJs      Generate a migration file on Javascript instead of
                      Typescript                      [boolean] [default: false]
      --dr, --dryrun  Prints out the contents of the migration instead of
                      writing it to a file            [boolean] [default: false]
      --ch, --check   Verifies that the current database is up to date and that
                      no migrations are needed. Otherwise exits with code 1.
                                                      [boolean] [default: false]
  -t, --timestamp     Custom timestamp for the migration name
                                                       [number] [default: false]
  -v, --version       Show version number                              [boolean]

Not enough non-option arguments: got 0, need at least 1
[~/tmp/MyProject]$ npx typeorm migration:generate test -d ./src/data-source.ts                                                                                                                                                         
Error during migration generation:
Error: Unable to open file: "/Users/vladsharikov/tmp/MyProject/src/data-source.ts". Cannot use import statement outside a module
    at Function.loadDataSource (/Users/vladsharikov/tmp/MyProject/node_modules/typeorm/commands/CommandUtils.js:22:19)
    at async Object.handler (/Users/vladsharikov/tmp/MyProject/node_modules/typeorm/commands/MigrationGenerateCommand.js:68:26)

My Environment

Dependency Version
Operating System macos
Node.js version v14.17.3 / v16.17.1
Typescript version 4.8.4
TypeORM version 0.3.11

Additional Context

I have tried typeorm-esm (I just accidentally found typeorm-ts-node-esm in docs and assumed there should be typeorm-esm module.

I have tried node --experimental-modules ./node_modules/.bin/typeorm migration:generate PostRefactoring -d ./src/data-source.ts. The same result as in the "Actual result" section.

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?

No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

UPD:

  • I can start a PR for the docs part
  • I am not sure I can start a PR for "Cannot use import statement outside a module" because I don't understand the root of the problem now. That should be a problem connected with the modules system. Like we are trying to use import in commonjs environment (nodejs). That is probably the cause.
@alexander-kraev-snyk
Copy link
Contributor

Hi @sharikovvladislav !

If you use ESM type imports you can try running it with ts-node.

Here is migration generation command for your case:

npx ts-node ./node_modules/.bin/typeorm migration:generate test -d ./src/data-source.ts  

@klangenk
Copy link

klangenk commented Feb 1, 2024

We could fix this problem by using typeorm-ts-node-commonjs like mentioned in the docs.

@alper
Copy link

alper commented Feb 15, 2024

@alexander-kraev-snyk So I do that:

npx ts-node ./node_modules/.bin/typeorm migration:generate -d ./src/config/typeorm.ts ./src/migrations/

And then I get:

/Users/alpercugun/Documents/projects/nestjs/bookstore-nest/node_modules/.bin/typeorm:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Object.require.extensions.<computed> [as .js] (/Users/alpercugun/Documents/projects/nestjs/bookstore-nest/node_modules/.pnpm/ts-node@10.9.1_@types+node@20.10.3_typescript@5.3.2/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at phase4 (/Users/alpercugun/Documents/projects/nestjs/bookstore-nest/node_modules/.pnpm/ts-node@10.9.1_@types+node@20.10.3_typescript@5.3.2/node_modules/ts-node/src/bin.ts:649:14)
    at bootstrap (/Users/alpercugun/Documents/projects/nestjs/bookstore-nest/node_modules/.pnpm/ts-node@10.9.1_@types+node@20.10.3_typescript@5.3.2/node_modules/ts-node/src/bin.ts:95:10)

@alper
Copy link

alper commented Feb 15, 2024

It also breaks if I do:
pnpm exec ts-node ./node_modules/.bin/typeorm migration:generate -d ./src/config/typeorm.ts ./src/migrations/

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

4 participants