question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to import image from Public folder as a Typescript module?

See original GitHub issue

I 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): Screenshot from Gyazo

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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
glebkudrcommented, Nov 5, 2018

Thx, it is working, partially…

  1. 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.

  2. 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?

0reactions
stale[bot]commented, Dec 11, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found