0
1

Jetbrains WebStormで"type": "module"指定されたnodejsプロジェクトをtsーnodeでデバッグする方法

Posted at

割とはまり込んだので、後刻の備忘録として

想定

プロジェクトのフォルダ構成は以下の通り

root:
│  package-lock.json
│  package.json
│  tsconfig.json
│
├─.idea
|  ...
│
└─src
        index.ts

Package.jsonは以下の通り

{
  "name": "untitled",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-node": "^10.9.2",
    "typescript": "^5.3.3"
  }
}

普通にtsconfig.jsonをこさえると以下のようになる

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

これだとmoduleがcommonjsなので、ここをnode16nodennextにする。

書き換えた後は以下

{
  "compilerOptions": {
    "target": "es2020",
    "module": "Node16",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

Run/Debug Confの調整

index.tsを実行したいとする場合、

JavaScript fileにsrc/index.tsを指定する。加えてNode parameterに
--import "data:text/javascript,import { register } from 'node:module'; import { pathToFileURL } from 'node:url'; register('ts-node/esm', pathToFileURL('./'));"
を指定する。

こうすることで、Execute/Debugが可能になる。

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