1. 程式人生 > >vscode下ts-node傳入cli引數

vscode下ts-node傳入cli引數

ts-node寫ts,啟動時習慣在package.json裡寫

  "scripts": {
    "build-ts": "tsc",
    "start": "ts-node src/server.ts",
    "start:dev": "nodemon",
    "serve": "node dist/server.js",
    "test": "mocha --require ts-node/register test/**/*.ts",
    "build_browser_dev": "browserify ./src/page.js -o ./static/bundle.js -t [ babelify --presets[env]]"
  },

但是當希望啟動時傳入命令列引數時,ts-node沒法接受命令列引數

https://www.npmjs.com/package/ts-node

Programmatic

You can require ts-node and register the loader for future requires by using require('ts-node').register({ /* options */ }). You can also use file shortcuts - node -r ts-node/register or node -r ts-node/register/transpile-only

 - depending on your preferences.

Note: If you need to use advanced node.js CLI arguments (e.g. --inspect), use them with node -r ts-node/register instead of the ts-node CLI.

然後給出段不明配置檔案

Visual Studio Code

Create a new node.js configuration, add -r ts-node/register

 to node args and move the program to the args list (so VS Code doesn't look for outFiles).

{     "type""node",     "request""launch",     "name""Launch Program",     "runtimeArgs"[         "-r",         "ts-node/register"     ],     "args"[         "${workspaceFolder}/index.ts"     ] }

不知該放那裡,說得不清楚。

參考這個vscode 除錯node之npm與nodemon,原來是vscode的launch.json