Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knexfile Not Using TS Paths

I'm trying to run a migration. My Knexfile is written in Typescript with some path aliases coming from tsonfig.json, but Knex fails with Cannot find module '@foo-alias/bar-module'. What do I need to change in the way I'm running Knex migrate or in my TS configuration to make that work?

I've tried using something like this, per some threads I've seen online, but no dice:

TS_NODE_PROJECT=tsconfig.knexfile.json knex migrate:latest

{
  "compilerOptions": {
    ...a bunch of compiler options here
    "paths": {
      "@foo-alias/*": ["src/foo/*"]
    }
  },
  "exclude": ["node_modules"]
}
like image 245
S. C. Avatar asked Jul 03 '26 09:07

S. C.


2 Answers

I just needed to register the tsconfig paths like so:

ts-node -r tsconfig-paths/register ./node_modules/.bin/knex --knexfile ./knexfile.ts "migrate:latest"
like image 78
S. C. Avatar answered Jul 06 '26 00:07

S. C.


if you dont want to modify the npm scripts

Add this to the top of the knexfile file

require('tsconfig-paths/register');
like image 37
abhijithvijayan Avatar answered Jul 06 '26 02:07

abhijithvijayan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!