#svg -> The requested module 'XXX' does not provide an export named 'ReactComponent'

12 messages · Page 1 of 1 (latest)

hot harness
#

I'm trying to view my component in Storybook, and I'm using Vite.

The full error occurs when trying to view this component in Storybook, there seems to be an issue with importing an SVG as a ReactComponent:

The requested module '/src/components/atoms/phones/Apple/iPhone12/with-frame.svg?import' does not provide an export named 'ReactComponent'

IPhone.tsx

import React from 'react'
import { ReactComponent as Frame} from './with-frame.svg'

export default function IPhone () {
  return (
    <div className='IPhone'>
      <Frame></Frame>
    </div>
  )
}

vite.config.ts

import { defineConfig } from 'vite'
import eslint from 'vite-plugin-eslint'
import react from '@vitejs/plugin-react-swc'
import svgr from "vite-plugin-svgr";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [eslint(), react(), svgr()],
})

src/custom.d.ts

declare module '*.svg' {
  import React = require('react');
  export const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>;
  const src: string;
  export default src;
}
tsconfig.json
{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "types": ["vite-plugin-svgr/client"]
  },
  "include": ["src", "src/custom.d.ts"],
  "references": [{ "path": "./tsconfig.node.json" }]
}
iron briar
#

are are trying to import a JS outbject from a SVG image file

#

that won't work

#

an image (whether .jpg .png or .svg) is still an image

#

it does not contain any code

#

TS "lets" you import it, so it will be bundled with your code

#

but you can't import a ReactComponent from it

#

I know you might not be using ESBuild, but the bundler you are using will do the same

#

it will see you're trying to import an image, and understgand that image needs to be converted to base64 and added to your code

#

might be more fitting as an exampole