How to import image from Public folder as a Typescript module?
See original GitHub issueI have a default create-react-app setup with Typescript.
My assets located in the public folder.
I want to load image into canvas through another library (Phaser).
And that works:
let img = './assets/sprites/mushroom.png';
Phaser.game.scene.load.image('monster', img);
I can’t find the mushroom.png in the output folder assets, but it loads somehow (actually it is here):
What I want now is to check whether all my assets present while compiling time and make an aliases for them.
So I want to do
import * as img from './assets/sprites/mushroom.png'
And it says: Module not found: Can’t resolve ‘./assets/sprites/mushroom.png’
I also have a file src/images.d.ts with declare module '*.png'
I have seen a multiple questions about that on SO, but can’t find an appropriate solution. Can you help me with this?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Importing images in TypeScript React - "Cannot find module"
Solution #4: Create declaration.d.ts in your src folder. Then add following text in it. declare module ...
Read more >How to use files in public folder in ReactJS ? - GeeksforGeeks
Output: Now open your browser and go to http://localhost:3000/, you will see the following output: For including any js files from the Public...
Read more >Using the Public Folder - Create React App
Normally we recommend importing stylesheets, images, and fonts from JavaScript. The public folder is useful as a workaround for a number of less ......
Read more >Serving Static Resources in Node.js - TutorialsTeacher
var express = require('express'); var app = express(); //setting middleware app.use(express.static(__dirname + 'public')); //Serves resources from public folder ...
Read more >HTML and Static Assets - Vue CLI
The file public/index.html is a template that will be processed with ... and CSS @import are resolved as module dependencies.
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
Thx, it is working, partially…
I do need to
declare module '*.png';
. WIthout it typescript says it can’t find a module mushroom.png on its exact location. I double-checked it.If I do (1) it works. But only when I use img in react tag
<img src={img}/>
. It contents BASE64 encoded string so I can’t use that string as a source for my library, it needs path. I’ve seen a documentation about 10kb treshhold in webpack config but I don’t want to eject it. Is there a way to overcome it?This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.