Node.js รัน TypeScript โดยตรง v23.6+: node app.ts ไม่ต้อง tsc/esbuild
กลับหน้ารายการบทความ

Node.js รัน TypeScript โดยตรง v23.6+: node app.ts ไม่ต้อง tsc/esbuild

Node.js 23.6+ รัน .ts/.mts/.cts native type stripping ESM/CommonJS production setup VS Code config migration guide performance vs tsx ts-node

Software อัพเดต: 8 มกราคม 2569

Node.js รัน TypeScript โดยตรงตั้งแต่ v23.6.0 - node app.ts ไม่ต้องคอมไพล์!

Node.js 23.6+ รองรับไฟล์ .ts/.mts/.cts แบบ native ด้วย type stripping ลบ type annotation รันทันที รองรับ ESM/CommonJS

Node.js TypeScript Support

ไฟล์รองรับหมายเหตุ
.tsStandard TypeScript
.mtsES Module TS
.ctsCommonJS TS
.tsxReact JSX

รัน TypeScript ทันที

# ง่ายสุด
node app.ts

# ESM project
node --loader tsx app.ts

# Experimental features
node --experimental-transform-types app.ts

ตัวอย่างใช้งานจริง

app.ts

// Type annotations ลบอัตโนมัติ
interface User {
id: number;
name: string;
}

async function getUser(id: number): Promise<User> {
return { id, name: `User ${id}` };
}

// รันได้เลย node app.ts
const user = await getUser(1);
console.log(user);

package.json

{
"type": "module",
"scripts": {
"dev": "node --watch app.ts",
"start": "node app.ts"
}
}

ESM Import ต้องระบุ .ts

// ✅ ถูกต้อง
import { handler } from './routes.ts';
import express from 'express';

// ❌ Error - ไม่รู้ extension
import { handler } from './routes';

ข้อจำกัดสำคัญ

❌ ไม่มี type checking (ต้อง tsc --noEmit)
❌ tsconfig.json ถูก ignore
❌ Decorators/JSX ต้อง flag
❌ ไม่แปลง const enum/private fields
✅ Syntax TS 99% รองรับ

Production Setup แนะนำ

// tsconfig.json (type check เท่านั้น)
{
"tsBuildInfoFile": ".cache/tsbuildinfo",
"noEmit": true,
"strict": true
}
// package.json
{
"scripts": {
"typecheck": "tsc --noEmit",
"dev": "tsx watch src/index.ts",
"start": "node dist/index.js",
"build": "tsc"
}
}

Performance Comparison

MethodCold StartMemoryBuild Step
node app.ts25ms80MB❌ None
tsc → node35ms75MB✅ Type check
tsx45ms120MB⚠️ Runtime

VS Code Config

// settings.json
{
"typescript.preferences.includePackageJsonAutoImports": "on",
"typescript.suggest.autoImports": true,
"node.experimentalTypeAcquisition": true
}

Migration Guide

1. Node.js >= 23.6.0 ✅
2. package.json "type": "module"
3. ระบุ .ts extension ทุก import
4. tsc --noEmit (type check)
5. node app.ts 🚀
D

DRITESTUDIO

บริษัท ไดรท์สตูดิโอ จำกัด - ผู้ให้บริการ Cloud, VPS, Hosting และ Colocation ในประเทศไทย

จัดการการตั้งค่าคุกกี้ของคุณ

เราใช้คุกกี้หลายประเภทเพื่อเพิ่มประสิทธิภาพประสบการณ์ของคุณบนเว็บไซต์ คลิกที่หมวดหมู่ด้านล่างเพื่อเรียนรู้เพิ่มเติมและปรับแต่งการตั้งค่า โปรดทราบว่าการบล็อกคุกกี้บางประเภทอาจส่งผลต่อประสบการณ์ของคุณ

คุกกี้ที่จำเป็น

คุกกี้เหล่านี้จำเป็นสำหรับการทำงานของเว็บไซต์ ช่วยให้ฟังก์ชันพื้นฐานเช่นการนำทางหน้าเว็บและการเข้าถึงพื้นที่ที่ปลอดภัย

ดูคุกกี้ที่ใช้
  • คุกกี้เซสชัน (การจัดการเซสชัน)
  • คุกกี้ความปลอดภัย (การป้องกัน CSRF)
เปิดเสมอ

คุกกี้ฟังก์ชัน

คุกกี้เหล่านี้ช่วยให้ฟีเจอร์ส่วนบุคคลเช่นการตั้งค่าภาษาและธีมทำงานได้ หากไม่มีคุกกี้เหล่านี้ ฟีเจอร์บางอย่างอาจทำงานไม่ถูกต้อง

ดูคุกกี้ที่ใช้
  • lang (การตั้งค่าภาษา)
  • theme (โหมดมืด/สว่าง)

คุกกี้วิเคราะห์

คุกกี้เหล่านี้ช่วยให้เราเข้าใจว่าผู้เข้าชมโต้ตอบกับเว็บไซต์อย่างไรโดยรวบรวมและรายงานข้อมูลแบบไม่ระบุตัวตน

ดูคุกกี้ที่ใช้
  • _ga (Google Analytics)
  • _gid (Google Analytics)

คุกกี้การตลาด

คุกกี้เหล่านี้ใช้เพื่อติดตามผู้เข้าชมข้ามเว็บไซต์เพื่อแสดงโฆษณาที่เกี่ยวข้องตามความสนใจของคุณ

ดูคุกกี้ที่ใช้
  • คุกกี้โฆษณา
  • พิกเซลรีมาร์เก็ตติ้ง

นโยบายความเป็นส่วนตัว