【问题标题】:Import module in typescript file gives error "cannot compile modules unless the '--module' flag is provided"打字稿文件中的导入模块给出错误“除非提供'--module'标志,否则无法编译模块”
【发布时间】:2016-03-03 01:06:52
【问题描述】:

我正在尝试导入全局安装 gulp 模块,但在 Visual Studio 代码中出现错误“无法编译模块,除非提供 '--module' 标志”

在哪里设置这个 --module 标志?

【问题讨论】:

标签: javascript node.js typescript gulp visual-studio-code


【解决方案1】:

打开您的tsconfig.json 文件并确保您已设置module

{
  "compilerOptions": {
    "module": "commonjs"
  }
}

模块的可能值列表是:

  • commonjs
  • amd
  • 系统

注意如果您已经设置了module,您可能需要通过命令托盘重新加载编辑器

另外值得注意的是--module 是指在引入tsconfig.json 之前的命令行参数,它被命名为module

【讨论】:

  • 我一直只有一个文件,它位于“文件”下的 tsconfig 中,编译不会出现此错误。打开 ts 文件会用红色下划线整个文件出现这个错误并且 ctrl+P >Reloac Window 不能解决问题。其他文件没有这个。尝试重新启动 IDE 并重新启动计算机,但它仍然显示错误(仅在 VS 代码中,而不是编译输出,并且仅针对该文件)
【解决方案2】:

我遇到了编译器输出未指示错误但 VS Code 指示错误的情况。

具有以下 tsconfig.json:

{
    "compilerOptions": {
        "target": "ES5",
        "watch": true,
        "module": "amd",
        "removeComments": true,
        "outDir": "."
    },
"files": [
        "Filter",
        "util.ts"
        ]
}

编译器没有错误,可以同时编译 Filter.ts 和 util.ts 但因为 tsconfig.json 在 Filter 中没有 .ts 扩展名;打开 util.ts 时,它会将整个文件内容用红色下划线,并带有 “除非提供 '--module' 标志,否则无法编译模块”

通过在末尾添加.ts 来修复它。

更改 tsconfig.json 后我重新加载了 ctrl + p => >Reload Window

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2013-08-27
    • 2019-12-30
    相关资源
    最近更新 更多