11
6

More than 1 year has passed since last update.

TypeScript: ESモジュールのTypeScriptをNodeで直接動かす方法

Posted at

TypeScriptをコンパイルのステップなしに、Node.jsで動かす方法として、よく知られているものに[ts-node]があります。ts-nodeはnodeコマンドのようなノリでTypeScriptファイルを実行できます。

# JavaScriptファイルの実行
node hoge.js

# TypeScriptファイルの実行
npx ts-node hoge.ts

ts-nodeがサポートしているのは、モジュールシステムがCommonJSのTypeScriptのみです。ES Modules(ESM)のTypeScriptはうまく動きません。

tsxならESM版TypeScriptを動かせる

ts-nodeの競合ツールに、tsxというものがあります。これを使うと、ES ModulesのTypeScriptが楽に実行できます。

npx tsx hoge.ts

ts-nodeで苦労して動かしていたESMが嘘のように簡単に動きます。

ちなみに、tsxというのは、TypeScript Executeの略です。

11
6
0

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
11
6