1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

最小構成でTypeScriptを動かす(console.logを出す)手軽で最短な方法

1
Last updated at Posted at 2026-05-17

細かいことはさておき

tsconfig.jsonとかはさておき。

console.logを動かしたい

これを実行させたい。

console.log("TypeScript動きました!");

前提知識

TypeScriptはJavaScriptにコンパイルされてから動作します。

Node.jsを入れます

JavaScriptをローカル実行させたいのでNode.jsを入れます。

WindowsならNode.jsのexeをダウンロードしてインストール。
Linuxならyum install nodejs npmなどでインストール。

TypeScriptも入れます

TypeScriptなので入れます。

好きなディレクトリを切って、
その場所でnpm install typescriptでインストール。

TypeScriptのコンパイラの場所

TypeScriptのコンパイラは、
上記コマンドを打ったときにできる
node_modulesの中にインストールされます。

node_modules/.bin/tscがコンパイラです。

TypeScriptのファイルを作る

index.tsみたいな名前のファイルを作って、こんな感じで書き込みます。

console.log("TypeScript動きました!");

TypeScriptをコンパイルしてJavaScriptにします

./node_modules/.bin/tsc index.ts

これで、index.tsがコンパイルされて、
同じディレクトリにindex.jsというファイルが出来ます。

コンパイルされたJavaScriptを実行します

node index.js

そしたら
TypeScript動きました!
と出ます。

最後に

npm install typescriptでTypeScriptをインストールしたとき勝手に出来る
package.jsonというファイルの中はこんな風になってますが、

{
  "dependencies": {
    "tsc": "^2.0.4",
    "typescript": "^6.0.3"
  }
}

ここにscriptsというのを以下のように3行足してあげると、

{
  "scripts": {
    "debug": "tsc index.ts && node index.js"
  },
  "dependencies": {
    "tsc": "^2.0.4",
    "typescript": "^6.0.3"
  }
}

npm run debugと打つだけでコンパイルと実行を同時にしてくれるようになります。

index.tsの中身を以下のように修正して、

console.log("コマンド一発で動きました!");

npm run debugと打つと「コマンド一発で動きました!」と出ます。

おしまい。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?