TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.8k stars 531 forks source link

ERR_UNKNOWN_FILE_EXTENSION no matter what I do. #2033

Open futuremotiondev opened 1 year ago

futuremotiondev commented 1 year ago

Search Terms

ERR_UNKNOWN_FILE_EXTENSION Unknown file extension ".ts"

I've tried re-installing ts-node over and over again, both globally and in scope of my project. No matter what I do, I hit the ERR_UNKNOWN_FILE_EXTENSION wall.

I've read through the documentation here about the error, and none of the suggestions have fixed my problem.

I've tried:

ts-node-esm ExportSVGsFromJSON.ts ts-node --esm ExportSVGsFromJSON.ts

I am on v10.9.1 of TS-Node.

You have moved your project to ESM but still have a config file, such as webpack.config.ts, which must be executed as CommonJS

I have not moved my project to ESM and have no config other than tsconfig.json.

Essentially, none of the suggestions have helped.

The kicker is that I actually had this working fine a few hours ago, but now its failing after a fresh reinstall.

Expected Behavior

ts-node .\ExportSVGsFromJSON.ts executes my TypeScript file.

Actual Behavior

ts-node .\ExportSVGsFromJSON.ts throws a ERR_UNKNOWN_FILE_EXTENSION error. The complete error is here:

07-04 06:46:58 D:\Dev\Node\Projects\Iconify> ts-node .\ExportSVGsFromJSON.ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for D:\Dev\Node\Projects\Iconify\ExportSVGsFromJSON.ts
    at new NodeError (node:internal/errors:399:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:139:38)
    at defaultLoad (node:internal/modules/esm/load:83:20)
    at DefaultModuleLoader.load (node:internal/modules/esm/loader:320:26)
    at DefaultModuleLoader.moduleProvider (node:internal/modules/esm/loader:195:22)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at DefaultModuleLoader.#createModuleJob (node:internal/modules/esm/loader:219:17)
    at DefaultModuleLoader.getJobFromResolveResult (node:internal/modules/esm/loader:172:34)
    at DefaultModuleLoader.getModuleJob (node:internal/modules/esm/loader:157:17) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Steps to reproduce the problem

npm install @iconify/tools --save npm install @iconify/utils --save npm i @iconify/types npm install typescript --save-dev npm install -D ts-node (or Globally, the result is the same)

Run ts-node .\ExportSVGsFromJSON.ts

Minimal reproduction

Install Iconify utils and tools, typescript, and ts-node. Try to run a TS script with ts-node.

Specifications

{
   "compileOnSave": true,
   "compilerOptions": {
      "target": "es6",
      "module": "commonjs",
      "declaration": false,
      "noImplicitAny": false,
      "moduleResolution": "node",
      "preserveConstEnums": true,
      "removeComments": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": true,

      "lib": ["es2015", "dom"],
      "baseUrl": "./",
      "typeRoots": [
          "node_modules/@types"
      ],
      "types": [
        "node"
      ],
      "paths": {
        "src/*": ["src/*"]
      }

   },

   "exclude": [
        "node_modules",
        "dist",
        "build"
    ],
    "filesGlob": [
        "./src/**/*.ts",
        "./test/**/*.ts",
        "./typings/index.d.ts",
        "./custom_typings/**/*.d.ts",
        "./node_modules/@types/**/*.d.ts"
    ]
}

I could really use some help here. I had this working nearly 2-3 hours ago and I must have messed something up. I also noticed that @iconify/json was updated in the last 6 hours. Maybe that has something to do with it.

If someone can point me in the right direction that would be a lifesaver.

Thank you.

RSchneider94 commented 1 year ago

hey @fmotion1 seems to be a known problem already, I have found the solution here on another thread https://github.com/TypeStrong/ts-node/issues/1997#issuecomment-1537111441 and it worked for me.

futuremotiondev commented 1 year ago

@RSchneider94

I managed to solve it:

  1. Removed "type": "module" from package.json
  2. Changed the "module" value in tsconfig.json to "module": "commonjs" along with some other changes
  3. Removed all global installs of Typescript and ts-node
  4. I now run my command with npx ts-node '.\ExportSVGsFromJSON.ts'

This is what worked for me. I think the key was removing the module declaration from package.json.

Here's my full package.json:

{
  "dependencies": {
    "@iconify/json": "^2.2.85",
    "@iconify/tools": "^3.0.2",
    "@iconify/types": "^2.0.0",
    "@iconify/utils": "^2.1.7",
    "iconify": "^1.4.0",
    "winston": "^3.9.0"
  },
  "scripts": {
    "dev": "npx ts-node ./ExportSVGsFromJSON.ts"
  },
  "devDependencies": {
    "@types/node": "^20.3.3",
    "ts-node": "^10.9.1",
    "typescript": "^5.1.6"
  }
}

And here's my full tsconfig.json:

{
    "compilerOptions": {
        "target": "es6",
        "skipLibCheck": true,
        "lib": [
            "ES6",
            "dom"
        ],
        "rootDir": ".",
        "module": "commonjs",
        "esModuleInterop": true,
        "moduleResolution": "node",
        "strict": true,
        "declaration": false,
        "sourceMap": true,
        "inlineSources": true,
        "types": [
            "node"
        ],
        "stripInternal": true,
        "incremental": true,
        "skipDefaultLibCheck": true,
        "inlineSourceMap": false,
        "noEmit": false
    },
    "include": [
        "./**/*"
    ],
    "exclude": [
        "node_modules"
    ],
    "ts-node": {
        "cwd": ".",
        "projectSearchDir": ".",
        "require": [],
        "project": "./tsconfig.json"
    }
}

I try to avoid installing global packages for portability concerns.

Really hope this manages to help some folks out there.

Edit:

tsx also works fine for me out of the box.

RSchneider94 commented 1 year ago

@fmotion1 hmmm yeah, removing the "type": "module" should work for sure.. but the problem is that some projects need this, because we are using/importing some libs that requires us to use this.. and then it's where the problems starts, and as far as I researched until now, the link I have sent above is the only way to make it work again, it seems that something broke with Node v20

ThePlenkov commented 1 year ago

I think is a bug. I also tried all the variants and ts-node doesn't work with type: module. However when I tried to run node --loader ts-node/esm index.js with code like

import 'index.ts'

then it works as expected

jszklarz commented 11 months ago

Specifications

  • ts-node version: v10.9.1
  • node version: v20.1.0 (But I have nvm with two other versions)
  • TypeScript version: v5.1.6
  • tsconfig.json, if you're using one:

I experienced this just now and it was due to being on node20. Going back to node18 solved it for me.

SalahAdDin commented 5 months ago

No any solution yet???

wolfy1339 commented 5 months ago

This has worked for me on Node 20

node --no-warnings=ExperimentalWarning --loader ts-node/esm file.ts