javascript knexfile can't run typescript migrations
See original GitHub issueEnvironment
Knex version: 0.21.1 Database + version: postgres 12 OS: Linux
Bug
- 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.
- 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>)
- 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
- 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.
- 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:
- Created 3 years ago
- Reactions:6
- Comments:20
For those seeing
for me the fix was that I was missing ts-node from my package.json. A simple
was all I needed.
@abcd-ca You can use:
knex uses ts-node’s require hook for
.ts
extension and that can accept TS compiler options throughTS_NODE_COMPILER_OPTIONS
env variable. These options will be merged into what you have in your tsconfig.json.