Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EsLint "Unable to resolve path to module" error in version 4.0 #91

Closed
kolorfilm opened this issue Sep 20, 2023 · 6 comments
Closed

EsLint "Unable to resolve path to module" error in version 4.0 #91

kolorfilm opened this issue Sep 20, 2023 · 6 comments

Comments

@kolorfilm
Copy link

I get a typescript error when try to import a svg like this:

import Close from './close.svg?react';

ESLint: Unable to resolve path to module './close.svg?react'.(import/no-unresolved)

@chrismeyers
Copy link

chrismeyers commented Sep 22, 2023

This is an ESLint error, not a TypeScript error. It looks related to this issue: import-js/eslint-plugin-import#1739

The workaround that I'm using is to just omit the querystring by reverting the include pattern to what it was before:

// vite.config.js
import { defineConfig } from 'vite';
import svgr from 'vite-plugin-svgr';

export default defineConfig({
  plugins: [svgr({ include: '**/*.svg' })],
});

Then you can import without ?react:

import Close from './close.svg';

Alternativley, you can disable the ESLint rule for this specific case. The build will catch any missing SVG files and fail, but I prefer seeing the error through ESLint.

// .eslintrc.js
module.exports = {
  rules: {
    'import/no-unresolved': ['error', { ignore: ['\\.svg\\?react$'] }]
  },
};

@kolorfilm kolorfilm changed the title Typescript "Unable to resolve path to module" error in version 4.0 EsLint "Unable to resolve path to module" error in version 4.0 Sep 22, 2023
@DevPowers
Copy link

@chrismeyers Using the proposal of :

plugins: [svgr({ include: '**/*.svg' })],

When importing the SVG, the IDE thinks I am importing a string and not a component giving Typescript errors.

@chrismeyers
Copy link

@DevPowers Maybe try setting declare module "*.svg" somewhere in your project to what it was before (see client.d.ts diff)?

@kolorfilm
Copy link
Author

kolorfilm commented Oct 2, 2023

@DevPowers yeah what @chrismeyers said:

you can add something like:

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

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

  export default ReactComponent;
}

to your vite-env.d.ts file.

@DevPowers
Copy link

That got rid of the import warning but when using the SVG component I am still getting issues "Property  style  does not exist on type  IntrinsicAttributes"

I am using the component like this, and passing style to control the height and width of the SVG image:

<SvgComponent
            aria-label="Logo"
            style={{
                height,
                width
            }}
        />

For now I can use the eslint ignore option and just import using the ?react option, but it seems a bit hacky. Would be nice if it played nice with typescript and eslint out of the box

@issam-seghir
Copy link

using this plugin: [svgr({ include: '**/*.svg' })], in vite config works great for me,
i don't need to use ?reactsyntax anymore, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants