Javascript Typescript:导入使用module.exports导出的内容
我有以下主题文件,出于某种原因需要用ES5编写:Javascript Typescript:导入使用module.exports导出的内容,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我有以下主题文件,出于某种原因需要用ES5编写: module.exports.theme={ 原色:“蓝色”, 边框大小:“2px”, // ... } 我想将其导入一个Typescript文件(在createreact应用程序环境中)。因此,我做了以下工作: 从“../../../../session/theme”导入{theme}; 这将导致以下错误: Attempted import error: 'theme' is not exported from '../../../../s
module.exports.theme={
原色:“蓝色”,
边框大小:“2px”,
// ...
}
我想将其导入一个Typescript文件(在createreact应用程序环境中)。因此,我做了以下工作:
从“../../../../session/theme”导入{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。 试试这个 否则,您需要使用ES6编译模块,您是说“使用ES5编译”,还是说“使用ES5编译”?
const theme = require('../../../../session/theme').theme;