Skip to content

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" in Node v21.5.0 #2100

Open

Description

@cibilex

First of all ,I already looked at #1997 and most of other websites to solve the problem but I couldn't.This error is throwed when I try to use in Node v21.5.0.I realized that The problem is node version,when I updated Node version to v19.0.0 it works.Could you please update ts-node to work in new Node versions.

Best regards.

Activity

  1. AaronNGray commented on Jan 15, 2024

    @AaronNGray

    Getting same issue, but I have a working version that I solved this issue on, and now a new project on which it does not. It is something else and I cannot remember what it is. Will report back once I have debugged the issue.

  2. AaronNGray commented on Jan 16, 2024

    @AaronNGray

    Heres a working example with node v21.5.0, typescript v5.3.3, ts-node v10.9.2 :-
    https://github.com/AaronNGray/ts-node-example

    NOTE: I have just found this does not seem to support imports !!!

  3. AaronNGray commented on Jan 16, 2024

    @AaronNGray

    First of all ,I already looked at #1997 and most of other websites to solve the problem but I couldn't.This error is throwed when I try to use in Node v21.5.0.I realized that The problem is node version,when I updated Node version to v19.0.0 it works.Could you please update ts-node to work in new Node versions.

    Could you please provide a minimal example of what does not work and then we can try to figure out why !

  4. peschee commented on Jan 16, 2024

    @peschee

    Heres a working example with node v21.5.0, typescript v5.3.3, ts-node v10.9.2 :- https://github.com/AaronNGray/ts-node-example

    This is not an ESM package. Try setting type to module in your package.json.

  5. AaronNGray commented on Jan 16, 2024

    @AaronNGray

    Heres a working example with node v21.5.0, typescript v5.3.3, ts-node v10.9.2 :- https://github.com/AaronNGray/ts-node-example

    This is not an ESM package. Try setting type to module in your package.json.

    AFAICS I am not sure ts-node supports ESM. Could you provide the example that works on node v19.0.0, but not v21.5.0. Or is it as simple as setting "type":"module" ? As in :-
    https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-module

    on v21.5.0 resulting in :-

    C:\Users\Nathaniel\test\Node.js\ts-node-example>npm start
    
    > ts-node-example@0.0.1 start
    > ts-node src/index.ts
    
    TypeError: Unknown file extension ".ts" for C:\Users\Nathaniel\test\Node.js\ts-node-example\src\index.ts
        at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
        at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
        at defaultLoad (node:internal/modules/esm/load:143:22)
        at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
        at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
        at async link (node:internal/modules/esm/module_job:76:21) {
      code: 'ERR_UNKNOWN_FILE_EXTENSION'
    }
    
  6. AaronNGray commented on Jan 16, 2024

    @AaronNGray

    Adding :-

    "start:node": "node --loader ts-node/esm src/index.ts"

    and tsconfig.json :-

    "module": "esnext"

    We have :-

    https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-loader

    which gives :-

    C:\Users\Nathaniel\test\Node.js\ts-node-example>npm run start:node
    
    > ts-node-example@0.0.1 start:node
    > node --loader ts-node/esm src/index.ts
    
    (node:18508) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
    --import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
    (Use `node --trace-warnings ...` to show where the warning was created)
    ts-node
    

    NOTE: I have just found this does not seem to support imports !!!

  7. cibilex commented on Jan 16, 2024

    @cibilex
    Author

    First of all ,I already looked at #1997 and most of other websites to solve the problem but I couldn't.This error is throwed when I try to use in Node v21.5.0.I realized that The problem is node version,when I updated Node version to v19.0.0 it works.Could you please update ts-node to work in new Node versions.

    Could you please provide a minimal example of what does not work and then we can try to figure out why !

    I've modified the project a bit and created new repository.link: https://github.com/cibilex/ts-node-err

    This project doesn't work in node v21.5.0 while works In node v16.20.2 for me.

  8. AaronNGray commented on Jan 16, 2024

    @AaronNGray

    First of all ,I already looked at #1997 and most of other websites to solve the problem but I couldn't.This error is throwed when I try to use in Node v21.5.0.I realized that The problem is node version,when I updated Node version to v19.0.0 it works.Could you please update ts-node to work in new Node versions.

    Could you please provide a minimal example of what does not work and then we can try to figure out why !

    I've modified the project a bit and created new repository.link: https://github.com/cibilex/ts-node-err

    This project doesn't work in node v21.5.0 while works In node v16.20.2 for me.

    You modified start to : -"start": "ts-node-esm src/index.ts"

    The version I posted last works AFAICT without proper testing, but I did do an import

    Note: --loader maybe deprecated.

    https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-loader

    NOTE: I have just found this does not seem to support imports !!!

  9. AaronNGray commented on Jan 16, 2024

    @AaronNGray

    @cibilex - Theres a pull request waiting :- #2073

  10. cibilex commented on Jan 17, 2024

    @cibilex
    Author

    @cibilex - Theres a pull request waiting :- #2073

    Thank you a lot,It works now splendid :)

  11. AaronNGray commented on Jan 17, 2024

    @AaronNGray

    @cibilex - Did you manage to add the two commits to a ts-node fork or did you just patch the diff's ?
    If you used git I would love to know how ! On the other front I seem to remember GitHub giving diffs but it does not seem to have downloads for them anymore. Or did you just use a reference to the repo in package.json dependencies ?

  12. cj-christoph-gysin commented on Jan 18, 2024

    @cj-christoph-gysin

    @cibilex - Did you manage to add the two commits to a ts-node fork or did you just patch the diff's ? If you used git I would love to know how !

    $ git fetch https://github.com/TypeStrong/ts-node refs/pull/2073/head
    $ git checkout -b pr-2073 FETCH_HEAD
    

    On the other front I seem to remember GitHub giving diffs but it does not seem to have downloads for them anymore.

    You can just add .diff or .patch to any github url for a commit or PR, e.g.: https://github.com/TypeStrong/ts-node/pull/2073.diff

  13. AaronNGray commented on Jan 18, 2024

    @AaronNGray

    @cibilex - Wow great thanks, think I was over complicating things :)

  14. MickL commented on Feb 14, 2024

    @MickL

    Guys no need for ts-node, just use Deno or Bun:

    deno run main.ts
    bun run main.ts
    
  15. russell-miburn commented on Feb 19, 2024

    @russell-miburn

    Is there an update on the progress of this bug?

  16. 53 remaining items

  17. heyjunpenn commented on Nov 11, 2024

    @heyjunpenn

    Node version 18.17.0 where ts-node with package.json "type"="module" work fine

  18. francip commented on Nov 22, 2024

    @francip

    @SoMeu yes, because the version that broke it is v18.19.0.

  19. hassamulhaq commented on Dec 7, 2024

    @hassamulhaq

    I was getting the same issue 'ERR_UNKNOWN_FILE_EXTENSION', and after two hours of investigation, I fixed it.

    How to fix [nodemon] app crashed error and unknown file extension issues?

    Note: i setup a fresh project using npm create vite@latest and then chose 'Vanillas -> TypeScript' options.

    my old node version was 20.18.0

    requirements

    `node -v` --> v22.12.0
    `npm -v`  --> 10.0.0
    

    update nodejs version in ubuntu

    sudo npm cache clean -f
    sudo npm install -g n
    sudo n stable
    
    sudo npm cache clean -f #[to clean npm cache]
    
    # in new shell type 'node -v' [importnat]

    create a nodemod.json file at root

    {
      "watch": ["src"],
      "ext": "ts,json",
      "ignore": ["node_modules"],
      "exec": "node --loader ts-node/esm --experimental-specifier-resolution=node src/index.ts",
      "legacyWatch": true
    }

    update package.json file

    ‎  "type": "module",
      "scripts": {
        "dev": "vite",
        "build": "tsc && vite build",
        "preview": "vite preview",
        "dev:server": "nodemon" #[add this line, so it'll trigger the nodemon.json]
      },

    create a file index.ts in src/ dir

    # src/index.ts
    
    import console from 'node:console';
    
    console.log("ts-node working!");

    now run nodemon dev server

    npm run dev:server
    
    # and in console you'll see "ts-node working!"
    # console will display some deprecated warnings, but your program will never break.

    my full package.json file

    {
      "name": "nodejs-crud-app",
      "private": true,
      "version": "0.0.0",
      "type": "module",
      "scripts": {
        "dev": "vite",
        "build": "tsc && vite build",
        "preview": "vite preview",
        "dev:server": "nodemon"
      },
      "devDependencies": {
        "nodemon": "^3.1.7", #[npm install --save-dev nodemon ts-node]
        "ts-node": "^10.9.2",
        "tsconfig-paths": "^4.2.0",
        "typescript": "~5.6.2",
        "vite": "^6.0.1"
      },
      "dependencies": {
        "@types/express": "^5.0.0", #[npm i @types/express]
        "@types/node": "^22.10.1",  #[npm i @types/node]
        "cors": "^2.8.5", #[npm i cors]
        "dotenv": "^16.4.7", #[npm i dotenv]
        "express": "^4.21.2", #[npm i express]
        "joi": "^17.13.3",
        "pg": "^8.13.1"
      }
    }

    extra

    npm install ts-node tsconfig-paths
    
    sudo npm cache clean -f #[clean npm cache]
  20. hicodingx commented on Dec 21, 2024

    @hicodingx

    I hope I found a way to handle that.
    Don't worry it works well so kindly give a like if it solve your problem.
    First, assure that in your package.json file, the type is set to module.

    "type": "module"

    Use the following configuration for in your tsconfig file :

    {
      "compilerOptions": {
        "skipLibCheck": true,
        "module": "ESNext",
        "moduleResolution": "bundler",
        "target": "ESNext",
        "isolatedModules": true,
        "esModuleInterop": true,
        "noEmit": true,
        "allowImportingTsExtensions": true,
        "outDir": "dist",
        "lib": ["esnext"],
        "types": ["node"],
        "baseUrl": "./"
      },
      "exclude": ["node_modules"],
      "include": ["src/**/*.ts", "bin/*.ts"]
    }

    Use this in your package.json to start the dev server :

     "dev": "node --loader ts-node/esm src/index.ts"

    Finally, be sure that you didn't forget to add file extension when making an import.

  21. vbudovski commented on Dec 23, 2024

    @vbudovski

    The only way I've managed to get this to work is by setting the loader or import argument via NODE_OPTIONS. Don't bother trying with ts-node-esm or ts-node --esm, it's too late by that point.

    1. Make sure your package.json contains "type": "module".
    2. Set "allowImportingTsExtensions": true in your tsconfig.json.
    3. Finally, you can can execute your TypeScript file like so: NODE_OPTIONS="--loader=ts-node/esm" npm exec ts-node index.ts
    4. The --import method is almost the same. Just create a file containing the registration script:
    import { pathToFileURL } from "node:url";
    import { register } from "node:module";
    
    register("ts-node/esm", pathToFileURL("./"));

    Then call it like so: NODE_OPTIONS="--import ./ts-node.register.mjs" npm exec ts-node index.ts

  22. soryy708 commented on Dec 29, 2024

    @soryy708

    In my case, on Node 22, there's no need for --loader.
    I use --experimental-strip-types, but got this same error.
    The solution was to make sure the CLI flag comes before the file to run.

    Works:

    node --experimental-strip-types ./src
    

    Doesn't work:

    node ./src --experimental-strip-types
    
  23. programmingwithsalim commented on Dec 30, 2024

    @programmingwithsalim

    npm i -D tsx
    npx tsx src/index.ts

    This worked for me

  24. LucasGarcez commented on Jan 2, 2025

    @LucasGarcez

    I just created an empty tsconfig.json file in the root folder and started to work. Nothing else.

    It's an example project to run TS, so I only have typescript as a dependency.

    {
      "dependencies": {
        "typescript": "^5.7.2"
      }
    }
    

    If it doesn't work for you, move on and use tsx It's also recommended in the Node.js docs: https://nodejs.org/en/learn/typescript/run#running-typescript-code-with-tsx

  25. heroku-postgres-wrong8 commented on Feb 9, 2025

    @heroku-postgres-wrong8

    @AaronNGray Fvh5ndurCipw

  26. yohanistadese commented on Feb 9, 2025

    @yohanistadese

    Here is completely work.

    1, First install this.

    • npm install -D tsx // install as development model

    2, After completely install:

    
    "scripts": {
      "dev": "npx tsx watch src/index.ts",
      "build": "tsc"
    },
    
    

    3, finally work correctly run npm run dev

    Note-> watch mode is used for automatically start your typescript application when something change in you code.

  27. DonnerFranzel55 commented on May 31, 2025

    @DonnerFranzel55

    I found a simpler way to run a TypeScript project with auto-reloading:

    1. Install dev dependencies:
    npm install --save-dev ts-node nodemon typescript
    1. Initialize TypeScript to generate a tsconfig.json:
    npx tsc --init
    1. Add the following scripts to your package.json:
    "scripts": {
      "start": "ts-node index.ts",
      "start:watch": "nodemon index.ts",
      "build": "tsc"
    }
    1. Run the project in watch mode:
    npm run start:watch
  28. reaishijie commented on Sep 10, 2025

    @reaishijie

    I found a simpler way to run a TypeScript project with auto-reloading:

    1. Install dev dependencies:

    npm install --save-dev ts-node nodemon typescript
    2. Initialize TypeScript to generate a tsconfig.json:

    npx tsc --init
    3. Add the following scripts to your package.json:

    "scripts": {
    "start": "ts-node index.ts",
    "start:watch": "nodemon index.ts",
    "build": "tsc"
    }
    4. Run the project in watch mode:

    npm run start:watch

    what's your package.json and tsconfig.json?

  29. DonnerFranzel55 commented on Sep 11, 2025

    @DonnerFranzel55

    I found a simpler way to run a TypeScript project with auto-reloading:

    1. Install dev dependencies:

    npm install --save-dev ts-node nodemon typescript
    2. Initialize TypeScript to generate a tsconfig.json:
    npx tsc --init
    3. Add the following scripts to your package.json:
    "scripts": {
    "start": "ts-node index.ts",
    "start:watch": "nodemon index.ts",
    "build": "tsc"
    }
    4. Run the project in watch mode:
    npm run start:watch

    what's your package.json and tsconfig.json?

    This is how a package.json could look like:

    {
      "name": "nodemon-with-ts",
      "version": "1.0.0",
      "description": "",
      "license": "ISC",
      "author": "",
      "type": "commonjs",
      "main": "index.ts",
      "scripts": {
        "start": "nodemon"
      },
      "devDependencies": {
        "nodemon": "^3.1.10",
        "ts-node": "^10.9.2",
        "typescript": "^5.9.2"
      }
    }

    and the tsconfig.json is the default generated from:

    tsc --init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions