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
javascript knexfile can't run typescript migrations #3849
Comments
@prk3 You could try with |
@love8587
|
Hello @prk3
|
@lorefnon that's right. I found out it from document and resolved it. |
I can't get it to run a knexfile.ts that has typescript syntax such as,
without the error,
I have tried running it like,
If I remove the import from knexfile.ts it just hides the problem because the rest of the file is the same as javascript – it creates the migration file with a .ts extension but any imports or requires of typescript code give the same error so I think we need to focus on why it's not interpreting knexfile.ts as typescript. Further, to get rid of the "Failed to load external module ts-node/register" message I did I don't know if TS is broken for the knex CLI or if I'm just missing something |
This was incorrect, I tried with 0.21.0 and it didn't fix it. I got the knexfile.ts to work with the |
Is there a way to have a separate tsconfig.json file just for the knex CLI as a workaround? |
@abcd-ca I resolved it below code.
|
@love8587 Yeah that's a good solution to your problem, thanks for sharing! In my case I realized it was a bit of a different problem than yours. In my case I couldn't get knex CLI to work with typescript files at all. In my case it was because my tsconfig.json didn't have |
@abcd-ca You can use:
knex uses ts-node's require hook for |
thanks! |
@lorefnon That gave me this alternative idea: created a tsconfig.knexcli.json containing an override for module,
Then when running my commands,
Just slightly cleaner because the JSON when in an npm script needs to escape the quotes and gets a bit busy looking. Thanks! |
@abcd-ca Yes, that certainly looks like a cleaner approach. |
Thanks for your solution @abcd-ca.
the workaround for 1 was changing to knexfile.ts, it should be enough for most people (as I've seen it, "commonjs" is the most common module resolution). but the workaround for 2 only indicates that we have issues with esm :) for ts/node maybe we need to run it with
somewhere down the pipeline? |
For those seeing
for me the fix was that I was missing ts-node from my package.json. A simple
was all I needed. |
I have the same issue, and your suggest to add ts-node works to me |
@abcd-ca's method of
in tsconfig.json worked for me as well |
I did all the things but this solved my issue. Thank you very much. Spent 3+ hours on this. |
I resolve with:
and add in package.json scripts: {
"scripts": {
...,
"knex": "TS_NODE_COMPILER_OPTIONS='{ \"module\": \"commonjs\" }' npx knex"
}
} now use |
Still having issues with this. This is the error I am getting:
|
I got it to work by generating a TS knexfile instead |
Environment
Knex version: 0.21.1
Database + version: postgres 12
OS: Linux
Bug
When the knexfile has
js
extension, typescript migrations and seeds can't be run. The error thrown suggests these files are not loaded with typescript loader. Changing knexfile extension tots
fixes the problem.This is rather unexpected, as the documentation suggests that knex can run migrations of different types, no matter what the knexfile extension is.
Feature discussion / request
I want to run Typescript migrations and seeds in development environment. In production, where Typescript and ts-node might not be present, compiled migrations and seeds should be used instead. Ideally, one
js
knexfile would handle both cases.Knexfile extension should not influence migration and seed loading. When
loadExtensions
includes.ts
, typescript migrations and seeds should be loaded and run.The text was updated successfully, but these errors were encountered: