Cannot find module "../static/icons/my-icon"
See original GitHub issueHi,
when I try to use any of the query params like “?url” I get an error Cannot find module "../static/icons/my-icon"
so this works:
<img src={require('../static/icons/my-icon.svg')} width="100%" height="100%"/>
… but this doesn’t:
<img src={require('../static/icons/my-icon.svg?url')} width="100%" height="100%"/>
(I’m using Typescript)
Thanks in advance!
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
44 - Stack Overflow
TS2307 : Cannot find module './images/logo.png'. All my other modules are importing just fine, ie; my css, svg and ts files. It only...
Read more >Cannot find module error when importing an static file using ...
bug report If you try to import an image, in normal javascript, to get the url of the file it just works: import...
Read more >Images, fonts, and assets - Storybook - JS.ORG
Components often rely on images, videos, fonts, and other assets to render as the user expects. There are many ways to use these...
Read more >Two ways to solve: Cannot find SVG module error when ...
Two ways to solve: Cannot find SVG module error when compiling Typescript in React application. If you import SVG file in React Typescript...
Read more >Static Asset Handling - Vite
png should be referenced in source code as /icon.png . Assets in public cannot be imported from JavaScript. new URL(url, import.meta.url) ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello, and sorry for the late reply. I was on vacation and didn’t take the laptop with me.
It looks like TypeScript doesn’t know about the webpack query parameters. I copy a typings file to the end of this issue, can you try to also create this in your project in
types/next-optimized-images.d.ts
and then rebuild the project? (you may also have another path for custom typing files depending on your setup) This at least solved a similar error for me. If this works for you too, I’ll try to add this file to the package itself so it should work out of the box. If it doesn’t solve your error, can you show me yournext.config.js
file (if there is nothing secret in it)?@genox in the readme, it is assumed that you have an
images
folder right in the same folder as your current react component is. If this is not the case, you have to change the import path just the same way as you would when you import another js file. I understand that there can be some confusion with the chosen paths and I’ll update this part of the readme for the next version.Thanks for the typings. Now I also understand why it didn’t work. 😃 I did in fact assume that assets are centralised, not per module. Depending on your architecture (e.g. ARC/atomic react components), this is unfeasible, but if it can be configured, that’s great. Will give it a try once the dependency things have been sorted out.