Assuming you've got a Next.js app that's already using TypeScript to render pages/components or handle API routes, but you also want to be able to run ad hoc build tasks written in TypeScript (perhaps pulling in type files used by the app)...
- Add these deps:
npm install -D @babel/core @babel/node
- Make sure your Next app has an explicit
.babelrc
file:
// Default Next.js .babelrc (it's OK if you also have extra stuff in it)
{
"presets": ["next/babel"],
"plugins": []
}
Now you can run .ts
files like this:
babel-node -x .ts myTask.ts
(Put that in a package.json script and run it with npm run taskname
, or prefix babel-node
with ./node_modules/.bin/
to run it directly.)
It is OK for your task to fail type-checking, it will still run.