无法从默认导出模块导入命名导出“XXX”(导入为“XXX”)(仅默认导出可用)

Eva*_*tti 8 javascript typescript reactjs uniswap

我在将其导入到我的 TypeScript React 项目中时遇到问题。

import { SwapWidget } from '@uniswap/widgets';
Run Code Online (Sandbox Code Playgroud)

编译时出现此错误yarn build

Can't import the named export 'SwapWidget' (imported as 'SwapWidget') from default-exporting module (only default export is available)
Run Code Online (Sandbox Code Playgroud)

这是库中的声明@uniswap\widgets

Can't import the named export 'SwapWidget' (imported as 'SwapWidget') from default-exporting module (only default export is available)
Run Code Online (Sandbox Code Playgroud)

ts-config.js:

declare type SwapWidgetProps = SwapProps & WidgetProps;
declare function SwapWidget(props: SwapWidgetProps): JSX.Element;

export { SUPPORTED_LOCALES, SwapWidget };
Run Code Online (Sandbox Code Playgroud)

Pet*_*man 0

对我来说,这个问题是由我的 webpack 规则之一将文件转换为node_modules.

我通过添加排除来修复它:

//webpack.config.js
module.exports = {
...
module:{
  rules:[
    {
      test: /\.(txt|glsl|vert|frag)/,
      type: 'asset/source',
      //add this exclude
      exclude: [/node_modules/],
    },
  ]
}
Run Code Online (Sandbox Code Playgroud)