knex: knex 0.16 doesn't support knexfiles written in TypeScript
Environment
Knex version: 0.16.3
Database + version: docker postgres:10.4
OS: Windows 10 Pro & Docker node:8.14.0
Bug
knex migrate:make --knexfile knexfile.ts- Error message:
Unexpected token import
Works normally on 0.15.x
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 7
- Comments: 89 (16 by maintainers)
Commits related to this issue
- Fix knex initialization Closes #2998 — committed to brunolm/knex by brunolm 5 years ago
- Fix `#listAllAndCompleted` not considering `loadExtensions` in migrator configuration (#2998) — committed to pvoisin/knex by deleted user 5 years ago
- Fix for #2998 - Migrator & TypeScript (#3041) * Fix `#listAllAndCompleted` not considering `loadExtensions` in migrator configuration (#2998) * Add unit test * Fix linting — committed to knex/knex by pvoisin 5 years ago
@brunolm why are you so ignorant?
I was facing the same issue with Knex version
0.16.3.This solution is not encouraged but I simply fixed it by simply adding
ts-node/registerat the beginning ofknexfile.tsfile.Solution
Instead of
--knexfileuse--cwdHi guys
I also had this error I think, the problem is that option
--knexfileis wrongly set a directory for knexfile.ts So I set explicit directions with--cwdfor the directory withknexfile.tsIt works for me:
"knex migrate:make --cwd src"(It is equal this:"cd src knex migrate:make")For those using
tsxruntime i usedand used it like
I still get this error with NodeJS 14.0.0, the command
knex migrate:make test, and the following file:I get this error:
EDIT: This was fixed by adding ts-node as a dependency. Sorry for the inconvenience.
Maybe it will help someone. Fixed when change in tsconfig.json “module”: “es2015” to “module”: “commonjs” Command in package.json => “migrate”: “ts-node -P ./tsconfig.json node_modules/.bin/knex migrate:latest --knexfile knexfile.ts”.
@brunolm @ShGKme @mmiszy @ilkka This should work better in 0.20.9 thanks to amazing work of @briandamaged. Please give it a try and let me know if the changes were helpful for you.
works 100% on
0.15Quick update on my end. Not sure if this is useful or not, but my issue was that I was using multiple
tsconfig.jsonfiles in my project, andknexwas using the default. When I overrode this functionality by directly launchingknexusingts-node, I was able to get around this error.My updated
package.json:How I run migrations now:
Hopefully this helps someone.
Thanks!!! It worked for me!
This still does not work predictably.
If the project uses tsconfig setting “module”: “ES2022” or similar at the root of the project ts-node will pick it up no matter if You would use --cwd or --knexfile and store the actual knexfile.ts in the nested directory. I presume ts compiler or ts-node itself will try to resolve the closest tsconfig.ts from parent directories.
This results in “Unexpected token bleh blah bling” types of errors.
The fix for me was to have the knexfile in nested directory, and create another tsconfig.json NEXT to the knexfile.ts and have the module property set to “CommonJS”.
Maybe possibility for user to give the used tsconfig as a parameter? Or maybe automatically overwrite the module to “CommonJS” if ts is detected so node can read it?
Anyhow, hopefully this helps someone…
@kibertoad + @brunolm : I’ll try to put something together either later today or tomorrow. I’m still making sure I understand the big picture first. From what I can tell, it looks like the Knex CLI might be duplicating some of the functionality that is already provided by the
Liftofflibrary.Open new issue and provide reproduction code (for example a link to example project where your problem occur). This was closed because @brunolm found his solution to his problem.
Thanks for looking into it. I’ll see if I can make liftoff behave in these cases 😄 but yeah, using ts-node directly should work in all cases.
In any case, thank you for reminding me about this issue. I’ll try to take another swing at it on the weekend, hopefully I can at least understand why it worked in the past 😄
@brunolm Haha, @briandamaged is the real hero. Glad it works fine now!
It’s still broken with
--cwd:Thanks !! It helps me a lot.
Hi guys. So as @mikl said, the problem is, that you are trying to run typescript code on node interpreter. I ran into this problem today when I was trying to create a new migration.
I’ve solved this issue by running knex through
ts-node(https://npmjs.com/package/ts-node).To make this work just add this script inside your
package.jsonfile 😃Replicate this to
migrate:latest,seed:runetc… 😃 Then just run your new script!For fellow googlers, I had the “unexpected token export” error when trying to migrate up/down with the latest knex 0.19 in full typescript mode.
I turned out I had both a
tsconfig.jsonand a.babelrcin the working directory, I suspect one of those interfered with the transpilation.When I moved the migration folder and the
knexfile.tsin a clean directory, it worked again ✅ .Had same error. Turned out I have copied wrong
tsconfig.json. Now it’s working. Relevant configs:package.json (workspace)
package.json (root):
(ts-node version is: 8.3.0)
tsconfig.json (workspace):
tsconfig.node.json (root):
src/knexfile.ts:
Command to run:
@algil I didn’t need to update or start a new project yet, so I’m still using 0.15 for the most part.
But I did make a PR that solves this issue, You can fork knex and apply these changes to it: https://github.com/tgriesser/knex/pull/3005
I think I found maybe a bug on 0.16, or just something weird.
On 0.15
launchis called withconfigPathhttps://github.com/tgriesser/knex/blob/0.15.2/bin/cli.js#L260But on 0.16 it’s called with
https://github.com/tgriesser/knex/blob/0.16.3/bin/cli.js#L303-L304
LiftOff calls
var env = this.buildEnvironment(opts);which callsfindConfig({passingconfigPath(which is no longer provided). Internally it usesconfigPathand makes no reference toknexfileorknexpath.I have
typescriptandts-nodeinstalled in the project and it works when I have v0.15 installed (like the example I provided on the Git repository.I did some debugging and found this, but I still didn’t figure out why it works on 0.15
knex@0.15.2Logging the result of this line
https://github.com/tgriesser/knex/blob/0.15.2/bin/cli.js#L55
I get this
The migration directory is saying
directory: '/usr/src/app/src/db/migrations1337',, which is exactly what I have on knexfile.tsAnd it’s directly requiring the file .ts
knex@0.16.3The require is to the exact same file, but it fails
after installing also some more packages (ts-node ts-node-dev typescript) it seems that both, 0.15 and 0.16 works just fine here… but indeed your docker setup with node 0.16 is failing and I have no idea why
I modified your docker-compose.yml to make pretty much the same and it fails like this: