knex: Broken CLI TypeScript support
Environment
Knex version: 0.20.15
Database + version: sqlite3
OS: macOS 10.14.6 (18G3020)
Example repository with bugs
https://github.com/htunnicliff/knex-cli-ts-errors
Bug (1)
Explain what kind of behaviour you are getting and how you think it should do
Running knex migrate:make some_migration_name with a knexfile.ts in the current working directory should produce a TypeScript migration file with no errors.
Error message
Failed to load external module ts-node/register
Failed to load external module typescript-node/register
Failed to load external module typescript-register
Failed to load external module typescript-require
Failed to load external module @babel/register
sqlite does not support inserting default values. Set the `useNullAsDefault` flag to hide this warning. (see docs http://knexjs.org/#Builder-insert).
Created Migration: /knex-cli-ts-errors/migrations/20200416104822_create_example_table.ts
Bug (2)
Explain what kind of behaviour you are getting and how you think it should do
Running knex migrate:up should migrate with no errors.
Error message
Failed to load external module ts-node/register
Failed to load external module typescript-node/register
Failed to load external module typescript-register
Failed to load external module typescript-require
Failed to load external module @babel/register
sqlite does not support inserting default values. Set the `useNullAsDefault` flag to hide this warning. (see docs http://knexjs.org/#Builder-insert).
/knex-cli-ts-errors/migrations/20200416104822_create_example_table.ts:1
import * as Knex from "knex";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1072:16)
at Module._compile (internal/modules/cjs/loader.js:1122:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at FsMigrations.getMigration (/knex-cli-ts-errors/node_modules/knex/lib/migrate/sources/fs-migrations.js:81:12)
at /knex-cli-ts-errors/node_modules/knex/lib/migrate/Migrator.js:146:69
at arrayFilter (/knex-cli-ts-errors/node_modules/lodash/lodash.js:582:11)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 16
- Comments: 25 (1 by maintainers)
Commits related to this issue
- Fix inferrence of seed/migration extension from knexfile extension Current implementation doesn't work when knexfile has multiple env-specific configuration sections. Resolves #3807 — committed to knex/knex by lorefnon 4 years ago
- Fix inferrence of seed/migration extension from knexfile extension Current implementation doesn't work when knexfile has multiple env-specific configuration sections. Resolves #3807 — committed to knex/knex by lorefnon 4 years ago
- Fix inferrence of seed/migration extension from knexfile extension Current implementation doesn't work when knexfile has multiple env-specific configuration sections. Resolves #3807 — committed to knex/knex by lorefnon 4 years ago
- Add documentation about liftoff and its dependencies. Resolves #3807 (#265) — committed to JakobJoonas/knex by lorefnon 4 years ago
- Move website (#5792) * Build documentation for 0.17.0 (#198) * Add documentation for migrate:down (#197) * add missing function to sidebar (#199) * Add notes around usage with typescript ... — committed to knex/knex by rluvaton 4 months ago
Found a work around here: https://github.com/knex/knex/issues/3849#issuecomment-643411244
in my
package.jsonadded this line, then executed whatever command I want:and run a command in command-line like:
Note: My problem was that
esnextis not supported for the knex cliWhat is the recommended workflow for running Typescript migrations? The documentation still suggests that running
knex migrate:latestis sufficient:See:
Workaround that worked for me using ts-node:
This successfully creates the migration file with a
.tsextension.As for running the migration, I still run them using the transpiled
.jsfiles via npm run scripts:I tried running the migrations as
.tsfiles before but I encountered weird runtime transpilation issues when running on deployed containers (not enough memory?).If this is truly the desired behavior, I think it is imperative that the docs elaborate on exactly how to get TypeScript working with ts-node. The docs don’t make a single mention of ts-node.
I use
env-cmdto load env vars from script"migrate": "env-cmd knex migrate:make -x ts"I think there are two things to be done here:
We need to document what is needed to interpret knexfile (or other files which knex imports) written in typescript - which is basically one of these modules.
Currently the utilities for inferring extension for migrations/seeds to be generated are not aware of knexfile extension where as IMHO they should default to knexfile extension in absense of explicit extension specified somewhere in config.
npm install ts-nodeworked for meremove “include” from “compilerOptions” in tsconfig.ts
Yea I am not a whiz with npm, guessing the argument is somehow not getting passed. Maybe you can try hardcoding the command in the package.json to see if it works.
No, this does not solve issue. I have ts-node installed locally and the issue still persists.
same error with ts-node: