cli icon indicating copy to clipboard operation
cli copied to clipboard

Typescript migrations doesn't work

Open andrejleitner opened this issue 3 years ago • 13 comments

What you are doing?

I have migrations written in typescript. These compiled worked fine until CLI of version 6.1.0. They don't after updating to version 6.4.1.

npx sequelize db:migrate --env development

What is actually happening?

ERROR: Cannot use import statement outside a module

Dialect: mysql Database version: 5.7 Sequelize CLI version: 6.4.1 Sequelize version: 6.15.1

andrejleitner avatar Mar 16 '22 12:03 andrejleitner

What does your TypeScript transpilation setup look like? Do you transpile your TS migrations to JS before running them?

It may be caused by this PR https://github.com/sequelize/cli/pull/915/, which loads TypeScript files but, unless ts-node is used, will treat them as plain JavaScript. IMO that PR should be rolled back and we should instead provide a way to configure which file extensions will be loaded & document how to use with ts-node.

ephys avatar Mar 16 '22 13:03 ephys

What does your TypeScript transpilation setup look like? Do you transpile your TS migrations to JS before running them?

Yep, I do (as it was necessary before the typescript was supported).

andrejleitner avatar Mar 16 '22 13:03 andrejleitner

Does your build output include both .ts (or .d.ts) & .js files? If so that's definitely caused by this PR.

ephys avatar Mar 16 '22 13:03 ephys

Yes, it does. But it fails also if I remove .js files (exclude form TS compilation).

andrejleitner avatar Mar 16 '22 14:03 andrejleitner

It's the .ts files that should be removed from the build output. At least until we have a solution

ephys avatar Mar 23 '22 10:03 ephys

IMO that PR should be rolled back and we should instead provide a way to configure which file extensions will be loaded & document how to use with ts-node.

Hey @ephys, any progress in here?☝🏻

andrejleitner avatar May 11 '22 06:05 andrejleitner

Summoning also @WikiRik, @sdepold, @sushantdhiman as you are probably the last active contributor here and "feat: support migrations files with ts file extension" is not actually true.

andrejleitner avatar May 11 '22 07:05 andrejleitner

Hey @ephys, any progress in here?☝🏻

Unfortunately my work on the CLI is limited to issue and PR review as the time I can donate to this project is limited and my focus is on the main library.

Hopefully someone else is willing to maintain the CLI :/

As for your issue, I think that if you remove the .ts files from your build output, and only keep the transpiled .js files there, it should work.

ephys avatar May 13 '22 13:05 ephys

As for your issue, I think that if you remove the .ts files from your build output, and only keep the transpiled .js files there, it should work.

Yep, but it doesn't actually mean TS migrations are supported. 🙂

andrejleitner avatar May 17 '22 07:05 andrejleitner

@andrejleitner did you find a solution to this issue?

ristiisa avatar Mar 16 '23 14:03 ristiisa

I still transpile .ts to .js myself.

andrejleitner avatar Mar 16 '23 15:03 andrejleitner

I too am facing the same error and use a different migrations directory to get around this problem

gousta avatar Feb 26 '24 14:02 gousta

Hi! I am too facing same issue 🆙 Workaround in build process to copy the migrations to different dir, and run the migrations from that directory - settting in .sequalizer

t3chnik avatar Feb 26 '24 14:02 t3chnik