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.

javascript knexfile can't run typescript migrations

See original GitHub issue

Environment

Knex version: 0.21.1 Database + version: postgres 12 OS: Linux

Bug

  1. Description

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 to ts 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.

loadExtensions: array of file extensions which knex will treat as migrations.
  1. Error message
import Knex from 'knex';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1047:16)
    at Module._compile (internal/modules/cjs/loader.js:1097:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Module.require (internal/modules/cjs/loader.js:1019:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at FsMigrations.getMigration (/home/przemek/DEV/sundae/sundae-collab-demo-api/node_modules/knex/lib/migrate/sources/fs-migrations.js:82:12)
    at /home/przemek/DEV/sundae/sundae-collab-demo-api/node_modules/knex/lib/migrate/Migrator.js:76:67
    at Array.some (<anonymous>)
  1. Knexfile content
require('dotenv').config();

const defaultSettings = {
  // client
  // connection
};

module.exports.development = {
  ...defaultSettings,
  migrations: {
    loadExtensions: ['.ts'],
    extension: 'ts',
  },
  seeds: {
    loadExtensions: ['.ts'],
  },
};

module.exports.test = {
  ...defaultSettings,
  migrations: {
    loadExtensions: ['.ts'],
    extension: 'ts',
  },
  seeds: {
    loadExtensions: ['.ts'],
  },
};

module.exports.production = {
  ...defaultSettings,
  migrations: {
    directory: 'build/migrations',
  },
  seeds: {
    directory: 'build/seeds',
  },
};

Feature discussion / request

  1. Explain what is your use case

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.

  1. Explain what kind of feature would support this

Knexfile extension should not influence migration and seed loading. When loadExtensions includes .ts, typescript migrations and seeds should be loaded and run.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:20

github_iconTop GitHub Comments

18reactions
Adverblycommented, Jul 31, 2020

For those seeing

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
Error: Cannot find module 'ts-node/register'
...
SyntaxError: Cannot use import statement outside a module

for me the fix was that I was missing ts-node from my package.json. A simple

npm install -D ts-node

was all I needed.

16reactions
lorefnoncommented, Jun 12, 2020

@abcd-ca You can use:

TS_NODE_COMPILER_OPTIONS='{ "module": "commonjs" }' knex migrate:latest

knex uses ts-node’s require hook for .ts extension and that can accept TS compiler options through TS_NODE_COMPILER_OPTIONS env variable. These options will be merged into what you have in your tsconfig.json.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not able to run migrations on Node.js with knex and ...
Hello! I have a Nest.js application running with docker and Postgres database I am using the multistage build for my docker image for ......
Read more >
TypeScript/Knex: Cannot use import statement outside a ...
The error tells us that Node.js and/or TypeScript think you're using CJS. ... to initialize knex, if you're creating typescript migrations:
Read more >
Migrations
Migrations use a knexfile, which specify various configuration settings for the module. To create a new knexfile, run the following:.
Read more >
Knex cheatsheet
Knex is an SQL query builder for Node.js. ... migration_name -x ts # Generates a TypeScript migration file knex migrate:latest knex migrate:rollback.
Read more >
tgriesser/knex - Gitter
I only have client and connection defined in knexfile.js , but I am using --esm . I'll check the issues. ... when I...
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