提问人:vishal1230 提问时间:11/10/2023 最后编辑:vishal1230 更新时间:11/10/2023 访问量:58
在“react-module”中找不到导出“default”(模块没有导出)
export 'default' was not found in 'react-module' (module has no exports)
问:
我使用 react TS 创建了一个 React 模块(react-accordion-animated),打包了 rollup。我将我的折叠组件放在 src 的 index.tsx 中并配置了汇总。
然后我做到了,现在我的另一个项目能够使用 locall 使用打包的 dist 文件夹,但它抛出错误模块没有导出。npm link
npm link ../path_to__another_reactapp_to_test/node_modules/react
我的模块目录是这样的:
react-accordion-animated
+--node_modules
+--src
+--+--index.tsx
+--+--index.css
+--+--add.svg
+--+--react-app-env.d.ts
+--dist
+--+--index.js
+--+--index.js.map
我的组件代码:
import * as React from 'react'
import { Add, Remove } from '@mui/icons-material';
import { Accordion as AccordionRootComponent, AccordionItem as Item } from '@szhsin/react-accordion'
import addIcon from "./add.svg"
import "./index.css"
const AccordionItem = () => { ... some item code }
function Accordion(props: {faqs: {
header: string,
content: string
}[],
headerStyle?: string,
contentStyle?: string,
expandButon?: number,
showBorder?: boolean
}) {
return (
<AccordionRootComponent transition transitionTimeout={200}>
{
props.faqs.map((faq, index) => (
<AccordionItem key={index} header={faq.header} headerStyle={props.headerStyle} contentStyle={props.contentStyle} showBorder={props.showBorder} expandButton={props.expandButon}>
{faq.content}
</AccordionItem>
))
}
</AccordionRootComponent>
)
}
Accordion.defaultProps = {
headerStyle: "",
contentStyle: "",
expandButon: 0,
showBorder: true
}
export default Accordion
我的汇总配置:
import sass from 'rollup-plugin-sass'
import typescript from 'rollup-plugin-typescript2'
// import tailwind from "rollup-plugin-tailwindcss"
import tailwind from "tailwindcss"
import tailwindConfig from './tailwind.config.js'
import autoprefixer from "autoprefixer"
import postcss from "rollup-plugin-postcss"
import svg from "rollup-plugin-svg"
import pkg from './package.json' assert {type: "json"};
export default {
input: 'src/index.tsx',
output: [
{
file: pkg.main,
format: 'cjs',
exports: 'named',
sourcemap: true,
strict: false
}
],
plugins: [
postcss({
extensions: ['.css', '.module.css'],
plugins: [autoprefixer(), tailwind(tailwindConfig)],
}),
typescript({ objectHashIgnoreUnknownHack: true }),
svg()
],
external: ['react', 'react-dom']
}
我的tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
],
"exclude": [
"node_modules",
"dist",
"example",
"rollup.config.js",
]
}
我正在测试项目中导入它,例如import Accordion from "react-accordion-animated";
我期望导入模块,传递 faqs 数组,就像 npm 注册表中的任何其他 react 模块一样
(我还尝试了命名导出和命名导入,但它抛出了同样的错误,模块没有导出)
答:
0赞
MrXQ
11/10/2023
#1
问题可能是在某些情况下,尤其是对于某些捆绑器或转译器,可能无法正确识别默认导出。
通过在索引中显式使用命名的导出语法。TSX 文件:
export { Accordion };
并导入它:
import { Accordion } from "react-accordion-animated";
您明确指出 Accordion 组件是命名导出,这在某些情况下可能未按预期处理默认导出会有所帮助。
评论
0赞
vishal1230
11/10/2023
尝试过,但是使用命名导出导入,它会抛出相同的错误,模块没有导出
评论