question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Broken CLI TypeScript support

See original GitHub issue

Environment

Knex version: 0.20.15 Database + version: sqlite3 OS: macOS 10.14.6 (18G3020)

@lorefnon

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)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:15
  • Comments:23 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
jowparkscommented, Aug 16, 2020

Found a work around here: https://github.com/knex/knex/issues/3849#issuecomment-643411244

in my package.json added this line, then executed whatever command I want:

scripts: {
  ...
  "api-knex": "TS_NODE_COMPILER_OPTIONS='{ \"module\": \"commonjs\" }' knex --knexfile=./path/to/knexfile.ts"
...

and run a command in command-line like:

npm run api-knex migrate:up

Note: My problem was that esnext is not supported for the knex cli

11reactions
YajJacksoncommented, Jul 22, 2020

What is the recommended workflow for running Typescript migrations? The documentation still suggests that running knex migrate:latest is sufficient:

Once you have a knexfile.js, you can use the migration tool to create migration files to the specified directory (default migrations). Creating new migration files can be achieved by running:

$ knex migrate:make migration_name 

# or for .ts

$ knex migrate:make migration_name -x ts

...

Once you have finished writing the migrations, you can update the database matching your NODE_ENV by running:

$ knex migrate:latest

See:

root@1e4d151bd246:/srv/app# node --version
v12.18.2
root@1e4d151bd246:/srv/app# npm run knex -- --version

> backend@0.1.0 knex /srv/app
> knex "--version"

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 sucrase/register/ts
Failed to load external module @babel/register
Knex CLI version: 0.19.5
Knex Local version: 0.19.5
root@1e4d151bd246:/srv/app#
root@1e4d151bd246:/srv/app#
root@1e4d151bd246:/srv/app#
root@1e4d151bd246:/srv/app# npm run knex -- migrate:make initial_migration -x ts

> backend@0.1.0 knex /srv/app
> knex "migrate:make" "initial_migration" "-x" "ts"

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 sucrase/register/ts
Failed to load external module @babel/register
Using environment: development
Using environment: development
Using environment: development
Created Migration: /srv/app/migrations/20200722212137_initial_migration.ts
root@1e4d151bd246:/srv/app# npm run knex -- migrate:latest

> backend@0.1.0 knex /srv/app
> knex "migrate:latest"

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 sucrase/register/ts
Failed to load external module @babel/register
Using environment: development
/srv/app/migrations/20200722212137_initial_migration.ts:1
import * as Knex from "knex";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1054:16)
    at Module._compile (internal/modules/cjs/loader.js:1102:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at FsMigrations.getMigration (/srv/app/node_modules/knex/lib/migrate/sources/fs-migrations.js:84:12)
    at /srv/app/node_modules/knex/lib/migrate/Migrator.js:82:69
    at arrayFilter (/srv/app/node_modules/lodash/lodash.js:582:11)
    at filter (/srv/app/node_modules/lodash/lodash.js:9173:14)
    at /srv/app/node_modules/knex/lib/migrate/Migrator.js:81:13
    at tryCatcher (/srv/app/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/srv/app/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/srv/app/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/srv/app/node_modules/bluebird/js/release/promise.js:649:10)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Versions of @angular/compiler-cli and typescript could not be ...
The most common reason for this is a broken npm install. Please make sure your package.json contains both @angular/compiler-cli and typescript ...
Read more >
Documentation - TypeScript 3.9
Visual Studio Code supports selecting different versions of TypeScript. ... This is a breaking change, but we believe most code was written with...
Read more >
Building a TypeScript CLI with Node.js and Commander
In this tutorial, we will talk about the CLI is and how to use Commander.js with TypeScript to build one. We will then...
Read more >
Announcing the Official TypeScript Types Public Preview
Ember Classic support. As specified in RFC 0800, there are also a number of breaking changes from the types in DefinitelyTyped regarding support...
Read more >
API - esbuild
The API can be accessed in one of three ways: on the command line, ... You may also find the TypeScript type definitions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found