Typescript -w(watch)在我的安装中不起作用

时间:2016-11-17 09:13:17

标签: javascript node.js typescript typescript2.0

我已经安装了Typescript 2.0.6,我也在安装节点。如果我尝试使用" -w"在监视模式下编译文件它不起作用。它抛出以下错误。

ts5001: the current host does not support the '--watch' option

使用以下命令编译我的打字稿文件" index.ts"

tsc -w index.ts

如果有任何需要,请帮助我如何在监视模式下编译代码。

4 个答案:

答案 0 :(得分:1)

使用watch

运行打字稿代码无法获得tsc功能

运行它 node tsc.js -w index.ts

来源:https://github.com/Microsoft/TypeScript/issues/2375

答案 1 :(得分:0)

我遇到了这个问题,对我来说,这是向我的环境变量/ PATH添加正确的路由的问题。打开“开发人员命令提示符”,然后输入“ where tsc”。您应该获得四种不同的路线:

1)C:\ Program Files(x86)\ Microsoft SDKs \ TypeScript \ 3.0 \ tsc.exe

2)C:\ Program Files(x86)\ Microsoft SDKs \ TypeScript \ 3.0 \ tsc.js

3)C:\ Users *您的用户名* \ AppData \ Roaming \ npm \ tsc

4)C:\ Users *您的用户名* \ AppData \ Roaming \ npm \ tsc.cmd

我最初的想法是将第一个添加到我的PATH中,但是使用“ tsc -w”所需的实际上是第三个。

因此将其添加到您的PATH:C:\ Users *您的用户名* \ AppData \ Roaming \ npm \

如果这是您的问题,那么您应该准备就绪。

答案 2 :(得分:0)

这是为我工作的解决方法:

  • 通过运行npm -i typescript
  • 在本地安装打字稿
  • 将运行npm脚本的watch任务添加到tasks.json(请参见下文)
  • watch脚本添加到packages.json(见下文)

要添加到tasks.json的任务:

{ "type": "npm", "script": "watch" } 要添加到packages.json的脚本

"watch": "node ./node_modules/typescript/bin/tsc -w"

答案 3 :(得分:0)

只是这个问题的更新答案


有一个内置的 `--watch` / `-w` 选项,请参阅 [TS 编译器选项](https://www.typescriptlang.org/docs/handbook/compiler-options.html)(在最后列表)。

或者一旦您使用 tsconfig.jsonoutDir 设置了 rootDir

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Basic Options */
    // "incremental": true,  /* Enable incremental compilation */
    "target": "ES2020",     /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "ES2020",   /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "outDir": "src/main/resources/static/js",  /* Redirect output structure to the directory. */
    "rootDir": "src/main/resources/static/ts" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
  }
}

然后你就可以打电话

tsc --watch

这第二个选项不是我个人尝试过的,但它是另一种文件监视选项,请参阅 [Configuring Watch](https://www.typescriptlang.org/docs/handbook/configuring-watch.html) 了解更多详细信息,但这里有从官方网站给出的示例 `tsconfig.json` 设置: <块引用>
{   // Some typical compiler options   
  "compilerOptions": {
    "target": "es2020",
    "moduleResolution": "node"
    // ...   },

  // NEW: Options for file/directory watching   "watchOptions": {
    // Use native file system events for files and directories
    "watchFile": "useFsEvents",
    "watchDirectory": "useFsEvents",

    // Poll files for updates more frequently
    // when they're updated a lot.
    "fallbackPolling": "dynamicPriority"   
  } 
}