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.

Can't import static files like images

See original GitHub issue

version: v2.1.1

additional webpack config to next.config.js:

config.module.rules.push(
        {test: /\.(png|jpeg)$/, loader: 'url-loader?limit=8192'}
    )

then in my component: import image from '../static/enen.png'

it comes the error:

This module was not found:

* /static/enen.png in ./pages?entry

To install it, you can run: npm install --save /static/enen.png
{ Error: Cannot find module '/static/enen.png'
...

I have tried to change the url as ‘/static/enen.png’ , and in the css-in-jsx:

p {
  background-image: url('../static/enen.png')
}
//or
p {
   background-image: $(require('../static/enen.png'))
}

all above comes the same error: can’t find the module, I can get it using <img src="../static/enen.png"/>

hope your help.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:40
  • Comments:51 (6 by maintainers)

github_iconTop GitHub Comments

205reactions
jpsccommented, May 17, 2017

If you add the image to the /static/ and then add the <img src="/static/image.png"/>? There is no need to load the image via the import.

171reactions
arunodacommented, May 18, 2017

@paulobarcelos it’s not a strong opinion. With our SSR setup, it’s kind a hard to it. (Since we don’t run webpack in the server) It’s doable, but we’ve much important other stuff to do.

@laoqiren why it’s not working simply use the image path like this:

background-image: url("/static/my-image.png")
Read more comments on GitHub >

github_iconTop Results From Across the Web

I can't reference an image in Next.js - Stack Overflow
Next.js can serve static files, like images, under a folder called public in the root directory. Files inside public can then be referenced ......
Read more >
Basic Features: Static File Serving - Next.js
js can serve static files, like images, under a folder called public in the root directory. Files inside public can then be referenced...
Read more >
How to manage static files (e.g. images, JavaScript, CSS)
Set the STATIC_ROOT setting to the directory from which you'd like to serve these files, for example: STATIC_ROOT = "/var/www/example.com/static/" · Run the ......
Read more >
Images, fonts, and assets - Storybook - JS.ORG
Images, fonts, and assets · Import assets into stories · Serving static files via Storybook Configuration · [⚠️ Deprecated] Serving static files via...
Read more >
Serving Static Files - Storybook
You can import any media assets by simply importing (or requiring) them as shown below. import React from 'react'; import { storiesOf }...
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