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.

knex 0.16 doesn't support knexfiles written in TypeScript

See original GitHub issue

Environment

Knex version: 0.16.3 Database + version: docker postgres:10.4 OS: Windows 10 Pro & Docker node:8.14.0

Bug

  1. knex migrate:make --knexfile knexfile.ts
  2. Error message: Unexpected token import

Works normally on 0.15.x

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:86 (16 by maintainers)

github_iconTop GitHub Comments

24reactions
Bessonovcommented, Jul 23, 2019

@brunolm why are you so ignorant?

diff --git a/api/package.json b/api/package.json
index c0f8bff..0906f51 100644
--- a/api/package.json
+++ b/api/package.json
@@ -8,7 +8,7 @@
     "dev": "ts-node-dev --respawn --poll --no-notify src/index.ts",
     "\n# Database": "",
     "migrate": "knex migrate:latest --knexfile ./src/db/knexfile.ts",
-    "migrate-make": "knex migrate:make --knexfile ./src/db/knexfile.ts",
+    "migrate-make": "knex migrate:make --cwd src/db",
     "seed": "knex seed:run --knexfile ./src/db/knexfile.ts",
     "seed-make": "knex seed:make --knexfile ./src/db/knexfile.ts",
     "\n# Testing": "",
api_1    | > knex16@1.0.0 migrate-make /usr/src/app
api_1    | > knex migrate:make --cwd src/db "test"
api_1    | 
api_1    | Requiring external module ts-node/register
api_1    | Working directory changed to /usr/src/app/src/db
api_1    | Created Migration: /usr/src/app/src/db/migrations/20190723173751_test.ts

18reactions
cham11ngcommented, Feb 12, 2019

I was facing the same issue with Knex version 0.16.3.

import * as Knex from 'knex';
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)

This solution is not encouraged but I simply fixed it by simply adding ts-node/register at the beginning of knexfile.ts file.

require('ts-node/register');
//
Read more comments on GitHub >

github_iconTop Results From Across the Web

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 ...
Read more >
Changelog - Knex.js
Fix error message for missing migration files #4937; Move deferrable to after on ... Add TypeScript support for deferrable, new Primary/Unique syntax #4589 ......
Read more >
0.16.3 - knex - npm
A SQL query builder that is flexible, portable, and fun to use! A batteries-included, multi-dialect (MSSQL, MySQL, PostgreSQL, SQLite3, Oracle ( ...
Read more >
Bookshelf.js | Home
js, built on the Knex SQL query builder. It features both Promise-based and traditional callback interfaces, transaction support, eager/nested-eager relation ...
Read more >
Knexfile Not Using TS Paths - node.js - Stack Overflow
My Knexfile is written in Typescript with some path aliases coming from tsonfig.json, but Knex fails with Cannot find module ...
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