gpt4 book ai didi

javascript - typescript : Importing something that was exported with module. 导出

转载 作者:行者123 更新时间:2023-12-03 14:22:33 24 4
gpt4 key购买 nike

我有以下主题文件,由于某种原因需要用 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"]
}

最佳答案

当你的模块使用 ES5 编译时,你必须使用 require 而不是 import。试试这个

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

否则你需要使用ES6编译模块

关于javascript - typescript : Importing something that was exported with module. 导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60402784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com