vite-plugin-svgr icon indicating copy to clipboard operation
vite-plugin-svgr copied to clipboard

vite, react-ts --- Module '"*.svg"' has no exported member 'ReactComponent'. ---

Open ElBambu opened this issue 1 year ago • 19 comments

Hello i just installed this module in a vite/react-ts project, i'm getting this error when i'm importing an SVG

import { ReactComponent as HomeIcon } from "images/icons/IconSvg/home.svg"

The import works and the icon is shown.

I managed to get rid of the error by declaring this module

declare module "*.svg" {
  import * as React from "react";

  export const ReactComponent: React.FunctionComponent<
    React.SVGProps<SVGSVGElement> & { title?: string }
  >;

  const src: string;
  export default src;
}

It doesn't really bother me, but i was wondering if there is a " cleaner " solution

Thanks for your responses :)

ElBambu avatar Jul 14 '22 13:07 ElBambu

If I understand it correctly, there is a section in the readme you can refer to:

If you are using TypeScript, there is also a declaration helper for better type inference:

/// <reference types="vite-plugin-svgr/client" />

and you don't need to declare it manually

pd4d10 avatar Jul 19 '22 12:07 pd4d10

You can add this to your tsconfig.json under compiler options and it will pick up the types

    "types": ["vite-plugin-svgr/client"]

If you are on VSCODE and it doesnt pick it up straight away, either restart vscode or ts server

Wiz1991 avatar Sep 14 '22 07:09 Wiz1991

pd4d10

Added inside client.d.ts: /// <reference types="vite-plugin-svgr/client" />

But got this error:

image

aslanalyiev avatar Oct 17 '22 04:10 aslanalyiev

Put this to your vite-env.d.ts

/// <reference types="vite-plugin-svgr/client" />

sedatbasar avatar Jan 18 '23 12:01 sedatbasar

I'm having a similar issue, except I can compile but I'm getting this in my browser console log:

Version: 2.4.0

Uncaught SyntaxError: ambiguous indirect export: ReactComponent

I have:

//vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import svgr from "vite-plugin-svgr";

export default defineConfig({
  plugins: [react(), svgr()],
  resolve: {
    alias: {
      src: "/src",
    },
  },
});
//vite-env.d.ts
/// <reference types="vite/client" />
/// <reference types="vite-plugin-svgr/client" />
// Component
import { ReactComponent as Icon} from '/src/assets/icon.svg';

I've also tried:

//vite-env.d.ts
"types": [
  "vite/client",
  "vite-plugin-svgr/client"
],

DigitalNaut avatar Jan 31 '23 00:01 DigitalNaut

Hey guys, I figured it out.

I bet all you guys struggled of this issue had set exportAsDefault to true.

The type definition of this library is export a variable called "ReactComponent" when you import a svg file, not default export.

declare module '*.svg' {
  import * as React from 'react'

export const ReactComponent: React.FunctionComponent<
    React.ComponentProps<'svg'> & { title?: string }
  >
}

so Don't set the exportAsDefault to true, which will export svg component as default export, which result in confilit with svg declaration from vite.

use this plugin like this:

import { ReactComponent as SvgIcon } from './xxx.svg';

panjiangyi avatar Jan 31 '23 01:01 panjiangyi

@panjiangyi So what's the point of exportAsDefault then?

lucsky avatar Feb 21 '23 23:02 lucsky

@lucsky IMO it gives you a choice, or compromise. If you think having to type .svg imports yourself is worth the effort to not need to rename the import every time, then exportAsDefault: true is for you.

h2thien avatar Mar 25 '23 03:03 h2thien

That being said, as an OSS, I think the best course of action here is for a community hero to provide the typing for when exportAsDefault: true. That is, when that option is enabled, you should only need to do:

/// <reference types="vite-plugin-svgr/client-default" />

h2thien avatar Mar 25 '23 03:03 h2thien

Does anyone have a way to get the vite-plugin-svgr/client types to take precedence over the vite/client types? My vite-env.d.ts looks like this:

/// <reference types="vite/client" />
/// <reference types="vite-plugin-svgr/client" />

And my svg imports are still typed as string instead of { ReactComponent }. Swapping the lines around doesn't work. Deleting the vite/client line does work, but then breaks my other modules' types.

MHebes avatar Jul 07 '23 17:07 MHebes

Add the following to your tsconfig:

{
  "compilerOptions": {
    "types": ["vite/client", "vitest/globals", "vite-plugin-svgr/client"],
    }
  }
}

spoilerdo avatar Aug 14 '23 08:08 spoilerdo

Have tried all suggested answers here and in documentation to no avail..

SleighJ avatar Oct 02 '23 21:10 SleighJ

Tried everything from here and from stackoverflow. Nothing works. I always end up with Uncaught SyntaxError: ambiguous indirect export: ReactComponent.

Also tried doing what OP did, but no luck. Also added reference types, but no luck.

currently vite.config.ts has this plugins: [react(), svgr({svgrOptions: {exportType: "named", ref: true}})], also played around with svgrOptions, but no luck.

I am really clueless after one day to trial and error.

truxiein avatar Oct 05 '23 22:10 truxiein

ReactComponent named export is for v3 and prior versions.

For the latest v4, please check out the document here: https://github.com/pd4d10/vite-plugin-svgr?tab=readme-ov-file#usage

Background here: https://github.com/pd4d10/vite-plugin-svgr/pull/71#issuecomment-1727006445

pd4d10 avatar Oct 06 '23 04:10 pd4d10

If using v4 of this package, you need to change your imports from

import { ReactComponent as Icon} from '/src/assets/icon.svg';

to

import Icon from '/src/assets/icon.svg?react';

MrRobz avatar Nov 06 '23 17:11 MrRobz

i hope this helps someone, i resolved this issue by moving vite-env.d.ts inside of my ./src directory

xxKeefer avatar Nov 10 '23 23:11 xxKeefer

If using v4 of this package, you need to change your imports from

import { ReactComponent as Icon} from '/src/assets/icon.svg';

to

import Icon from '/src/assets/icon.svg?react';

It looks like this syntax is not supported with Jest :(

chawax avatar Nov 13 '23 14:11 chawax

@chawax vitest-dev/vitest

sagarpanchal avatar Dec 15 '23 11:12 sagarpanchal

Tried everything from here and from stackoverflow. Nothing works. I always end up with Uncaught SyntaxError: ambiguous indirect export: ReactComponent.

Also tried doing what OP did, but no luck. Also added reference types, but no luck.

currently vite.config.ts has this plugins: [react(), svgr({svgrOptions: {exportType: "named", ref: true}})], also played around with svgrOptions, but no luck.

I am really clueless after one day to trial and error.

You need to add include: '**/*.svg' prop to svgr plugin configuration. Vite config should looks like this:

plugins: [
    react(),
    svgr({
      svgrOptions: { exportType: 'named', ref: true },
      include: '**/*.svg',
    }),
],

lukomwro avatar Jan 04 '24 18:01 lukomwro