No articles found
Try different keywords or browse our categories
Fix: ERR_UNKNOWN_FILE_EXTENSION '.ts' error
Quick fix for 'ERR_UNKNOWN_FILE_EXTENSION .ts' error in Node.js. Learn how to properly run TypeScript files in Node.js environments.
The ‘ERR_UNKNOWN_FILE_EXTENSION .ts’ error occurs when Node.js cannot recognize the ‘.ts’ file extension, typically because it doesn’t know how to handle TypeScript files without proper configuration or transpilation.
How the Error Happens
❌ Error Scenario:
# ❌ This causes the error
node src/index.ts
# Error: ERR_UNKNOWN_FILE_EXTENSION .ts
✅ Quick Fix - Handle TypeScript in Node.js
Solution 1: Use ts-node for Development
# ✅ Install ts-node globally or locally
npm install -g ts-node
# OR for local project
npm install --save-dev ts-node
# ✅ Run TypeScript files directly
npx ts-node src/index.ts
# OR if installed globally
ts-node src/index.ts
Solution 2: Transpile to JavaScript First
# ✅ Install TypeScript compiler
npm install -g typescript
# OR for local project
npm install --save-dev typescript
# ✅ Create tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true
}
}
# ✅ Compile TypeScript to JavaScript
tsc
# ✅ Run the compiled JavaScript
node dist/index.js
Solution 3: Use tsx for Modern Execution
# ✅ Install tsx (faster TypeScript execution)
npm install -g tsx
# OR for local project
npm install --save-dev tsx
# ✅ Run TypeScript files directly with tsx
npx tsx src/index.ts
# OR if installed globally
tsx src/index.ts
Solution 4: Configure Package.json for TypeScript
// ✅ Add to package.json
{
"name": "my-app",
"type": "module",
"scripts": {
"dev": "tsx watch src/index.ts",
"start": "tsx src/index.ts",
"build": "tsc"
},
"devDependencies": {
"tsx": "^4.0.0",
"typescript": "^5.0.0"
}
}
// ✅ For ES modules with .ts files, use loader
node --loader ts-node/esm src/index.ts Related Articles
Fix: ExperimentalWarning: Importing JSON modules error
Quick fix for 'ExperimentalWarning: Importing JSON modules' error in Node.js. Learn how to properly import JSON files in modern Node.js applications.
Fix: ERR_PACKAGE_PATH_NOT_EXPORTED error
Quick fix for 'ERR_PACKAGE_PATH_NOT_EXPORTED' error in Node.js. Learn how to resolve module export issues in ES modules and package configurations.
Fix: ERR_MODULE_NOT_FOUND Node.js error
Quick fix for 'ERR_MODULE_NOT_FOUND' error in Node.js. Learn how to resolve module resolution issues in ES modules and CommonJS.