Typescript:如何导入使用了module.exports导出的内容

3
我有以下主题文件,由于某些原因需要用ES5编写:
module.exports.theme = {
  primaryColor: 'blue',
  borderSize: '2px',
  // ...
}

我想在Typescript文件中导入它(在create-react-app环境中)。因此,我按照以下步骤操作:

import { theme } from "../../../../session/theme";

导致以下错误的原因是:
Attempted import error: 'theme' is not exported from '../../../../session/theme'.

我还试过使用module.exports.default = { ... }导出,并导入默认值,但它基本上也会显示相同的信息(没有默认导出)。

我该如何解决这个问题?

我的tsconfig.json文件看起来像这样:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": ["src"]
}
1个回答

1

由于您的模块是使用ES5编译的,因此您必须使用require而不是import。 请尝试这样做。

const theme = require('../../../../session/theme').theme;

否则,您需要使用ES6编译该模块。

说“与ES5编译”是指“编译为ES5”吗? - ostrebler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接