Help us understand the problem. What is going on with this article?

Node.jsで実行できるスクリプトをTypeScriptで書くテンプレート

More than 1 year has passed since last update.

概要

Node.jsで実行できるスクリプトをTypeScriptで書ける環境を作り,テンプレートにしました.

場所

https://github.com/kabosusoba-lab/typescript-cli-template

使い方

git clone https://github.com/kabosusoba-lab/typescript-cli-template.git tool
cd tool
npm install # インストール
npm run build # ビルド
node dist/main.js # 実行

以下,詳細な環境構築手順

検証環境

Macbook pro

環境構築

mkdir tool
cd tool/
git init
curl https://raw.githubusercontent.com/github/gitignore/master/Node.gitignore > .gitignore
npm init # 質問にはお好みに回答
npm install --save-dev webpack webpack-cli typescript ts-loader @types/node
./node_modules/typescript/bin/tsc --init

webpack.config.jsを作成

module.exports = {
  entry: './src/index.ts',
  target: 'node',
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: ['ts-loader'],
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ]
  }
};

package.jsonのscriptsを下記のように変更する.

{
    "scripts": {
        "build": "webpack",
    },
}
mkdir src
touch src/index.ts # ここへコードを書く

ビルドと実行

npm run build # ビルド
node dist/main.js # 実行

参考

TypeScriptでCLIツールを作って、npmパッケージにする

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away