DEV Community

Acid Coder
Acid Coder

Posted on

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
Enter fullscreen mode Exit fullscreen mode

setup npm script

"scripts": {
  "start": "babel-node --inspect -x .ts -- src/app.ts",
}
Enter fullscreen mode Exit fullscreen mode

create a babel.config.js

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

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

finally npm start and boom, it just works.

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

Discussion (0)