Can"t run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts
🚀 How to Fix the "Unknown file extension .ts" Error in a Node.js Typescript Project
So, you're trying to run your Node.js Typescript project on Heroku, but you're getting the dreaded TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts
error. Don't panic! This guide will help you troubleshoot and fix this issue in no time. Let's dive in!
⚡️ Understanding the Problem
The error message you're seeing indicates that the ts-node
module, which is responsible for executing Typescript files, is unable to recognize the .ts
file extension. This error usually occurs when the ts-node
module is not properly configured or when there's a mismatch between the Node.js version and the Typescript compiler.
🛠️ Solution 1: Update Dependencies
One common cause of this error is outdated or incompatible dependencies. To fix this, let's update the dependencies in your package.json
file:
"typescript": "^3.9.3",
"ts-node": "8.10.1",
Replace them with the latest versions:
"typescript": "^4.0.3",
"ts-node": "^9.0.0",
Save the package.json
file and run npm install
to update the dependencies.
🛠️ Solution 2: Configure ts-node
If updating the dependencies didn't solve the problem, let's try configuring ts-node
manually.
First, ensure that the ts-node
package is installed by running:
npm install ts-node
Next, create a tsconfig-node.json
file in the root directory of your project, and add the following configuration:
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowJs": true
}
}
Change the "start"
script in your package.json
file from "ts-node src/App.ts"
to:
"ts-node --project tsconfig-node.json src/App.ts"
Save the changes and try running your project again.
🛠️ Solution 3: Verify Node.js Version
Sometimes, the ERR_UNKNOWN_FILE_EXTENSION
error can occur if there's a mismatch between the version of Node.js you're using and the version of Typescript. Make sure that you're using the latest LTS version of Node.js and Typescript.
To check your Node.js version, run:
node --version
To check your Typescript version, run:
tsc --version
If your Node.js or Typescript versions are outdated, upgrade them to the latest LTS versions.
🎉 Time to Celebrate!
Now that you've followed these easy steps, you should be able to run your Node.js Typescript project on Heroku without the Unknown file extension ".ts"
error. Congrats! 🎉
If you have any other questions or issues, leave a comment below or reach out on our community forum. Happy coding!
✨ Keep exploring, keep learning, and keep coding! ✨