Home > OS >  TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" - Again
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" - Again

Time:08-22

I use Node.JS workspace with the following structure:

root/
    |- build/
    |- ModuleA/
         |- build/
    |- ModuleB/
         |- build/
    |- WebAPI/
         |- build/
         |- server.ts

build folder contains all the rollup output.

root package.json has "type": "module", which I don't want to remove because I want to use import

tsconfig.json has: "moduleResolution": "node" "esModuleInterop": true. "module": "ES2022"

When trying to run the web application npx ts-node ./WebAPI/server.ts, I bump into the error:

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

Running the app with ts-node-esm src/webapi/server.ts bumps into the following error:

CustomError: Cannot find module '/root/webapi/Presenters/RegisterUserPresenter'

on the line of import:

import { RegisterUserPresenter } from "../Presenters/RegisterUserPresenter"

What should I do?

CodePudding user response:

node --experimental-specifier-resolution=node build/src/webapi/server.js does the job!

CodePudding user response:

To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig.json file

npm install module-name

npm install --save-dev @types/module-name

Make sure to replace module-name with the name of the module in your error message.

tsconfig.json

{
  "compilerOptions": {
    "moduleResolution": "node",
    //.......
  }
}
  • Related