Member-only story
Node.js Full TypeScript Support: What You Need to Know
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.
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
andexport
syntax, add"type": "module"
to yourpackage.json
. - For CommonJS modules with
require
andmodule.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…