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.

migrate:make fails with TypeScript

See original GitHub issue

Environment

Knex version: 0.16.3 (Using TypeScript knexfile) Database + version: PostgreSQL 10.5 OS: Windows 10 Pro 64 Bit

Bug

When running knex migrate:make somename, this is the output:

Requiring external module ts-node/register
Using environment: development
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
    at assertPath (path.js:39:11)
    at Object.resolve (path.js:166:7)
    at directories.map.directory (X:\Code\node\knextest\node_modules\knex\lib\migrate\MigrationGenerator.js:82:28)
    at Array.map (<anonymous>)
    at MigrationGenerator._absoluteConfigDirs (X:\Code\node\knextest\node_modules\knex\lib\migrate\MigrationGenerator.js:81:24)
    at MigrationGenerator._ensureFolder (X:\Code\node\knextest\node_modules\knex\lib\migrate\MigrationGenerator.js:41:23)
    at MigrationGenerator.make (X:\Code\node\knextest\node_modules\knex\lib\migrate\MigrationGenerator.js:35:17)
    at Migrator.make (X:\Code\node\knextest\node_modules\knex\lib\migrate\Migrator.js:122:27)
    at Command.commander.command.description.option.action (X:\Code\node\knextest\node_modules\knex\bin\cli.js:179:10)
    at Command.listener (X:\Code\node\knextest\node_modules\commander\index.js:315:8)
    at Command.emit (events.js:182:13)
    at Command.parseArgs (X:\Code\node\knextest\node_modules\commander\index.js:654:12)
    at Command.parse (X:\Code\node\knextest\node_modules\commander\index.js:474:21)
    at Liftoff.invoke (X:\Code\node\knextest\node_modules\knex\bin\cli.js:278:13)
    at Liftoff.execute (X:\Code\node\knextest\node_modules\liftoff\index.js:203:12)
    at module.exports (X:\Code\node\knextest\node_modules\flagged-respawn\index.js:51:3)

This is the content of my knexfile.ts:

module.exports = {
  development: {
    client: 'pg',
    connection: {
      host: 'localhost',
      database: 'postgres',
      user: 'postgres',
      password: 'pw',
    },
    migrations: {
      directory: 'migrations',
    },
  },
};

Some additional details:

  • knex migrate:make somename works if I change knexfile extension to .js. (knex 0.16.3)
  • knex migrate:make somename works like it should with TypeScript knexfile when using knex 0.15.1 and @types/knex 0.15.1 package.
  • I have not tried different database engines or the versions between knex 0.15.1 - 0.16.3.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:27 (2 by maintainers)

github_iconTop GitHub Comments

35reactions
tecky-devcommented, Mar 7, 2019

We also use Knex for writing the query to a PostgreSQL server. We discovered that the best way to generate is as follow.

  1. Generate Migration File
knex migrate:make --knexfile knexfile.ts -x ts <your-migration-name>
  1. Run Migration File
knex migrate:latest --knexfile knexfile.ts 
  1. Generate seed File
knex seed:make --knexfile knexfile.ts -x ts <your-seed-name>
  1. Run seed File
knex seed:run --knexfile knexfile.ts 

The --knexfile knexfile.ts won’t be necessary if you use knexfile.js since the typescript cannot provide any type checking for you for the config file.

22reactions
elhigucommented, Jan 18, 2019

Sounds like automatic knexfile resolving does not look for knexfile.ts, you can pass --knexfile knexfile.ts argument to command or give path directly to the migration generator command with:

knex migrate:make --migrations-directory . -x ts migration-name

I suppose client should look for knexfile.ts automatically though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

knex & MYSQL js error ER_CANNOT_ADD_FOREIGN
I figured out it was how the files were made. Knex builds each table in the order you did this command js knex...
Read more >
Documentation - Migrating from JavaScript - TypeScript
How to migrate from JavaScript to TypeScript.
Read more >
Knex cheatsheet - Devhints
One-page guide to Knex: usage, examples, and more. Knex is an SQL query builder for Node.js.This guide targets v0.13.0.
Read more >
Migrations | Knex.js
knex migrate:make migration_name # or for .ts $ knex migrate:make ... If the config.migrations.directory is not defined, this operation will fail.
Read more >
Integration Tests with Jest, Supertest, Knex, and Objection in ...
npm i objection knex pg express npm i -D typescript jest ... findById(id) if (!book) { throw new Error('Book not found') } return ......
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