Skip to content

Using TypeScript

If you’re using NodeJS you have at least the two following options to support running TypeScript migration files in NodeJS.

Using tsx

If you want to be able to write and run migration files written in TypeScript an easy way is to install the tsx package.

Installing tsx

Terminal window
npm install tsx

Loading TypeScript migrations

After installing tsx you can load it in two ways.

Via CLI

Using the --import flag you can load tsx before running your migration files.

Terminal window
npx emigrate up --import tsx

Via configuration file

You can also directly import tsx in your configuration file (will only work if you’re not using TypeScript for your configuration file).

emigrate.config.js
import 'tsx';
export default {
// ...
};

Then you can run your migration files as usual:

Terminal window
npx emigrate up

Building TypeScript migrations

If you don’t want to have tsx (or similar) as a dependency included in your production environment then you can build your TypeScript migration files using the tsc compiler or some other tool that are already part of your build process when transpiling your TypeScript code to JavaScript.

Assume that you have all of your migrations in a src/migrations directory and you have built them to a dist/migrations directory.

Then you can run your migration files by pointing to the dist/migrations directory:

Terminal window
npx emigrate up -d dist/migrations