8

How would you adjust the following script to allow electron main process to use Typescript with ts-node?

"scripts": {
     "shell": "cross-env NODE_ENV=development electron ts-node ./app/main.ts"
}
Raathigesh
  • 2,306
  • 3
  • 26
  • 32
  • Isn't it more a question of doing `cross-env NODE_ENV=development ts-node electron ./app/main.ts`? I am asking because I haven't really tried it out, but seems logical. Otherwise you have to look at something like `ts-node/register` – Tokimon Nov 15 '17 at 23:28

2 Answers2

5

cross-env NODE_ENV=development electron -r ts-node/register ./app/main.ts

https://github.com/TypeStrong/ts-node#programmatic

You can require ts-node and register the loader for future requires by using require('ts-node').register({ /* options */ }). You can also use file shortcuts - node -r ts-node/register or node -r ts-node/register/transpile-only - depending on your preferences.

sanperrier
  • 606
  • 5
  • 12
0

While using electron -r ts-node/register works for simple cases, you can also have your script point to a bootscript JavaScript file that just does this:

require('ts-node').register()
require('./app/main')

The benefit of doing this is that you can specify ts-node options. Necessary for e.g. monorepos, where you might to something like require('ts-node').register({ project: './app/tsconfig.json' }).

See the ts-node docs for options that can be specified: https://typestrong.org/ts-node/api/interfaces/RegisterOptions.html

AndyO
  • 1,512
  • 20
  • 28