next.js: 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.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 39
- Comments: 51 (7 by maintainers)
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.@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:
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.
Hi
You can actually use webpack loaders in v5, someone already made a plugin for handling images: https://github.com/arefaslani/next-images
I don’t understand why would you want to use something like next-images or next-optimized-images if all they do is configuring file-loader/url-loader for you. These projects should be nothing more than gists people can contribute to and modify to their needs. Shouldn’t it output these files to
/publicfolder anyway according to https://nextjs.org/docs/basic-features/static-file-serving ? Because cmon…_next/static/isn’t even documented anywhere… This is definitely one hell of a mess looking at previous comments in this issue. Looking at the issues in linked projects I came to the assumption you’re better off just configuring webpack yourself…Tested this only in development so far but at least it outputs the files according to the official docs and it’s clear to understand. I will update it in case it doesn’t work in production.
By the way,
staticfolder was deprecated: https://nextjs.org/blog/next-9-1#public-directory-support@laoqiren The files must be within a
/static/directory relative to the pages directory. There are several examples of this working in the documentation.You can actually use webpack loaders in v5, someone already made a plugin for handling images: This is not working
@lededje I want to use a webpack/babel loader to require images in my components.
e.g:
<img src={require('./myImage.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.
See also: https://github.com/cyrilwanner/next-optimized-images
many of you are just saying things they dont understand here just to act woke, address the question at hand and move and stop raising questions and complicating issues.
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?
As @bugproof points out, you don’t need packages like next-images or next-optimized-images.
Here are steps on how you can import images. I copied how
create-react-appdoes this.npm i --save-dev url-loadertouch next.config.jsif you don’t already have a next.config.js file.If you’re coming here from google, this is what seams best as of now : next-optimized-images
@Italox hardcoding image urls doesn’t work in case images needs to be served from a different domain than the app itself (like CDN for example).
@ruhaise
Hey check this out, https://github.com/zeit/next.js/blob/master/errors/static-dir-deprecated.md
I just saw this in my console after digging for a good bit. Was able to get it working by adding a
publicfolder instead ofstaticand I am usingnext@v^9.1.1Have to second the sentiments from this comment https://github.com/vercel/next.js/issues/1935#issuecomment-648660605
This
Imagecomponent is a drag and an example of a framework being too heavy handed. Leave this abstraction up to the end user and get out of the way.Related: https://github.com/vercel/next.js/issues/18393
https://nextjs.org/docs/api-reference/next/image
👀 Not asking for these inline styles at all
How about a hook to build just the URL?
loaders not working with styled jsx
url('./lol.png')must be replace tohttp://localhost:3000/_next/static/chunks/images/lol-3cb46b372ab77d032576b9f70c83d1ff.pngbut this does not happenI’m using
next-optimized-imagesto import images, however it does not work in typescript, I’m getting messages like:Cannot find module './banner.png'.It does work with jsx though.In my app, I
<img src={require('images/myfile.png');} />with nextjs. The reasons I want to do this:In my
next.config.js, here’s an example of how i’ve added a rule for letting me require images in my source codeIt’s unclear to me when nextjs does things with webpack, so I added
emitFile: isServerin the hope that files will only be processed by webpack when the server bundle is built, and not duplicate work when the client bundle is built.It’s unclear to me if this pattern adds any overhead to the build, because I’m not sure what webpack is doing under the hood with images/static assets in dev mode.
It looks like you can add static files in general by following the pattern of the withSass
next-imagesplugin: https://github.com/twopluszero/next-images/blob/master/index.js and adding similar code for other file types in yournext.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. Usingstatic/by default is going back in time when we relied on global variables (path strings) to line up.@arunoda yes, It doens’t work just using
background-image: url("/static/my-image.png").This doesn’t really work. I tried it.
webpack loader fixed the problem, still typescript complains about it
Cannot find module '../images/me.png'.ts(2307)