首页 > 解决方案 > TS2306 不是打字稿文件上的模块错误

问题描述

所以我的打字稿文件出现了这个错误。我不明白为什么它不起作用。据我了解,这是导出课程所需要做的。但是我不断收到错误消息,指出我的文件不是模块并且无法导入。

错误

TS2306 不是打字稿文件上的模块错误
export default class GridApi{
    Hello(): string {
        return "Hello"
    }
}

这也是我的 tsconfig

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "CommonJS",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": false,
    "jsx": "react"
  },
  "exclude": [
    "node_modules",
    "./node_modules",
    "./node_modules/*"
  ]
}

标签: typescript

解决方案


您的module设置设置为CommonJS,这不是您想要的。CommonJS 模块使用require语法,但您使用import/export. 您应该改用ES6模块:

"module": "ES6",

在此处阅读有关 TSC 选项的更多信息。


推荐阅读