Babel Node + Typescript minus TS Node

If you want to use typescript with babel-node, here is how you do it:

install dependencies

npm i -D @babel/core @babel/node @babel/preset-env @babel/preset-typescript typescript

setup npm script

"scripts": {
"start": "babel-node -x .ts -- src/app.ts",
}

create a babel.config.js

module.exports = {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
}

create a src/app.ts, in this example I use koa

import Koa from 'koa'
const app = new Koa()
// response
app.use(ctx => {
ctx.body = 'Hello Koa'
})
app.listen(3000)

finally npm start and boom, it just works.

This solution comes in handy when you are tight on memory and you want to avoid ts-node

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Acid Coder

Typescript Zombie. Youtube Pikachu On Acid. (Unrelated to programming but by watching it you become a good developer overnight)