npm install --save ts-node
tsconfig.json
"module": "esnext",
↓
"module": "commonjs",
npx ts-node <実行したいtypescriptファイル名>
そのまま実行すると以下のようなエラーとなることがあります
(node:42851) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
その場合は実行用の tsconfig.json を作成してその設定ファイルを明示的に指定しながら実行します。
tsconfig.cli.json
{
"compilerOptions": {
// ts-node対応 esnext → NodeNext
"module": "NodeNext",
}
// ts-node
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
}
}
--project <設定ファイル名> を加えて実行します。
npx ts-node --project tsconfig.cli.json src/sample.ts