Home > OS >  Unknown file extension ".ts" error appears when trying to run a ts-node script
Unknown file extension ".ts" error appears when trying to run a ts-node script

Time:04-06

I'm trying to run a script created in a regular folder with two .ts files. One with the script and one with helper functions to run it. I'm also importing more things such as axios or form-data.

The thing is that when I try to run the script with ts-node: node script.ts, the following error appears:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

My package.json:

{
  "dependencies": {
    "@types/node": "^17.0.23",
    "axios": "^0.26.1",
    "form-data": "^4.0.0",
    "showdown": "^2.0.3",
    "ts-node": "^10.7.0",
    "typescript": "^4.6.3"
  },
  "type": "module"
}

And my tsconfig.json:

{
  "compilerOptions": {
    "esModuleInterop": true
  },
  "include": ["/**/*.ts"],
  "exclude": ["node_modules"]
}

My imports in the script.ts file are:

import { datoManagementPrimaryEnvironment } from "./content.management";
import {
  createContent,
  uploadToCloudfare,
  getEntryFromDatoWithTheId,
  getFilters,
} from "./helpers";

and in helpers.ts:

import { datoManagementPrimaryEnvironment } from "./content.management";
import axios from "axios";
import FormData from "form-data";
var showdown = require("showdown");

Does anyone know what I'm doing wrong? thanks!

CodePudding user response:

Remove "type": "module" from your package.json

And change your tsconfig.json to:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "moduleResolution": "node"
  },
  "include": ["/**/*.ts"],
  "exclude": ["node_modules"]
}

CodePudding user response:

When you want to use ts-node you should try running the file with ts-node scripts.ts instead of node scripts.ts.

You can also refer to the usage examples on npmjs.com

  • Related