Can't import static files like images
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.
Images by default are not accessible via import. You would need to modify the babelrc to allow for this. What data are you expecting to get returned from importing an image like that? a base64 encoded version of it? A browser resolvable path to it?
@lededje thanks for reply. so how to config this ? I havn't find this in the document. I'd appreciate it if you can give me an example ( base64 & resolvable path)
I would also like to know that. It seems the next team has strong opinions about using webpack for processing images, but it seems very limiting that you can't do it if you want to...
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.
@jpsc But this doens't work in css using styled-jsx.
@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")
@arunoda yes, It doens't work just using background-image: url("/static/my-image.png").
@arunoda See, we have a problem here which we're trying to solve, we're not looking for workarounds in which you're suggesting to use static images. how can we achieve requiring images (png, jpg, etc...) ? I personally prefer to have a customizable web pack for better customizability.
@mehdijjz what is it your are trying to achieve? It's difficult to know what to suggest when you don't explain what you want when importing images. The example given above doesn't have to be a CSS background image, it can be an image tag too.
@lededje I want to use a webpack/babel loader to require images in my components.
e.g: <img src={require('./myImage.png')} />
@mehdijjz That which runs on the server must also run on the client. This will not run on the client if you use webpack to perform this task. Here is a link to using a custom babel configuration but I must stress, I doubt you will find what you are looking for.
I ask once again, what do you expect require('./myImage.png') to return? I'm not trying to doubt your implemtation, just understand what you are trying to acheive to give you a better answer.
yes, It doens't work just using background-image: url("/static/my-image.png").
@laoqiren The files must be within a /static/ directory relative to the pages directory. There are several examples of this working in the documentation.
Thanks @lededje for all those references and details.
@mehdijjz there are a lot of things we like to do. But sometimes, we can't do all of them. But someday, we'll be able to do it.
In the meantime, you need live without those features.
add the following babel plugin: https://www.npmjs.com/package/babel-plugin-inline-import-data-uri
You can actually use webpack loaders in v5, someone already made a plugin for handling images: https://github.com/arefaslani/next-images
See also: https://github.com/cyrilwanner/next-optimized-images
You can actually use webpack loaders in v5, someone already made a plugin for handling images: https://github.com/arefaslani/next-images
This doesn't really work. I tried it.
/* eslint-disable */
const withSass = require('@zeit/next-sass');
const withImages = require('next-images');
module.exports = withSass({
sassLoaderOptions: {
includePaths: ['./styles/']
},
cssModules: true,
cssLoaderOptions: {
importLoaders: 1,
localIdentName: '[local]___[hash:base64:5]'
},
withImages: withImages()
});
Hi
You can actually use webpack loaders in v5, someone already made a plugin for handling images: This is not working
It looks like you can add static files in general by following the pattern of the withSass next-images plugin: https://github.com/twopluszero/next-images/blob/master/index.js and adding similar code for other file types in your next.config.js, or adding your own extension for it.
(I don't understand the hard coding of publicPath: '/_next/static/' into the above linked plugin, shouldn't this be from some config setting?)
I'm still playing with it, but I wish there was better first class support for this, or more documentation around it. Using a static/ folder defeats one of the core principles/reasons for using Webpack, which is a compile-time-provable dependency graph of all critical assets in your codebase. Using static/ by default is going back in time when we relied on global variables (path strings) to line up.