LoginSignup
8
9

More than 3 years have passed since last update.

TypeScriptで書いてwebpackでビルドしたjsファイルのエラーをわかりやすくする

Last updated at Posted at 2019-09-12

Why

webpackでバンドルしてるのでスタックトレースを見てもtsファイルでのエラー箇所がわからない・・ :tired_face:

What

node-source-map-supportをインストールして、tsconfig.json, webpack.config.jsを修正します :muscle:

node-source-map-supportのインストール

$ npm install source-map-support

tsファイルの修正

webpackのエントリポイントにしているtsファイルに以下を追加します。

app.ts
import sourceMapSupport from 'source-map-support'
sourceMapSupport.install()

設定ファイルの修正

webpack, TypeScriptの設定ファイルを修正します。

webpack.config.js
module.exports = {
    mode: 'development',
    target: 'node',
+   devtool: 'inline-source-map',
    ...
}

↑ちなみに、 devtool: 'inline-source-map'でも動きました。

tsconfig.js
{
  "compilerOptions": {
+   "sourceMap": true
   ...
  }
}

これで、webpackでビルドし直すと、エラー時にtsファイルの行数も教えてくれるようになりました :tada:

こんなかんじ :point_down: (app.tsの73行目でエラーになってることがわかる)

at Object.<anonymous> (/path/to/project/dist/webpack:/src/app.ts:73:4)

以上です :hugging:

参考

8
9
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
8
9