LoginSignup
19
7

ts-node の Unknown file extension ".ts" エラーを解決する

Last updated at Posted at 2023-01-25

概要

ts-node で TypeScript のコードを実行しようとしたとき以下のようなエラーが出ることがある。

npx ts-node src/index.ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/hoge/workspace/fuga/src/index.ts

調べてみると、解決策の1つとして package.json から "type": "module" を消せ、というのが挙げられていることがあるが、これは消したくない。
どうしたら良いか。

解決策 1

--esm オプションをつけて実行する

npx ts-node --esm src/index.ts

解決策 2

tsconfig.json に以下の記述を追加する。

tsconfig.json
{
  // other settings...
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node"
  }
}
19
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
7