ベスパリブ

プログラミングを主とした日記・備忘録です。ベスパ持ってないです。

`npm run tsc --init` でエラーが起きる原因

package.jsonのscriptsセクションが以下のようになっているとします。

  "scripts": {
    "tsc": "tsc"
  },

これで、シェル等で

> npm run tsc

とコマンドを実行すると実際には、

> ./node_modules/.bin/tsc

が実行されます。

このように、scriptsセクションでは、./node_modules/.bin/hogeと打つのがめんどうなコマンドをnpm run hogeで実行してくれるシュートカットを登録できます。

ということなので、npm run tsc --initとオプション引数を渡してコマンドを実行できそうですが、これをするとエラーが起きます。残念。

PS C:\Users\XXXX\workspace\myapp\myapp> npm run tsc --init

> myapp@1.0.0 tsc C:\Users\XXXX\workspace\myapp\myapp
> tsc

error TS18003: No inputs were found in config file 'C:/Users/XXXX/workspace/myapp/myapp/tsconfig.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'.


Found 1 error.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! myapp@1.0.0 tsc: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the myapp@1.0.0 tsc script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\XXXX\AppData\Roaming\npm-cache\_logs\2020-03-19T02_15_16_995Z-debug.log

コマンド引数を受け付けてくれないんですね。

しょうがないので、この場合のとりうる手段としては、

  • 【方法1】: 直接./node_modules/.bin/tsc --initとやる
  • 【方法2】: scriptsセクションにまんまのコマンドを追加する

のどちらかしかなさそうです。

方法1

> ./node_modules/.bin/tsc --init

方法2

  "scripts": {
    "tsc": "tsc",
    "tsc--init": "tsc --init"
  },
> npm run tsc--init