Member-only story

Node.js Full TypeScript Support: What You Need to Know

Emad Dehnavi
2 min readDec 30, 2024

As of Node.js v23.5.0, there is built-in lightweight support, you can use the built-in support for type stripping, and for full typescript support, you needt to use tsx . Let’s see what’s changed and how you can setup your Nodejs project to use Typesccript.

Node.js Full TypeScript Support: What You Need to Know

Running TypeScript Files Directly in Node.js

You can execute TypeScript files using the node command:

node --import=tsx your-file.ts 

Node.js determines the module system for .ts files based on the nearest package.json:

  • To use ES modules with import and export syntax, add "type": "module" to your package.json.
  • For CommonJS modules with require and module.exports, ensure the "type" field is either omitted or set to "commonjs".

Additionally, you can use specific file extensions to enforce module types:

  • .mts files are always treated as ES modules.
  • .cts files are always treated as CommonJS modules.

Limitations

While Node.js’s built-in TypeScript support is convenient, it may not cover all TypeScript features, such as certain type transformations or configurations specified in tsconfig.json. For comprehensive TypeScript support, consider using…

--

--

Emad Dehnavi
Emad Dehnavi

Written by Emad Dehnavi

With 8 years as a software engineer, I write about AI and technology in a simple way. My goal is to make these topics easy and interesting for everyone.

No responses yet