next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Can't import static files like images

Open laoqiren opened this issue 9 years ago • 51 comments

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.

laoqiren avatar May 10 '17 15:05 laoqiren

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 avatar May 11 '17 08:05 lededje

@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)

laoqiren avatar May 11 '17 09:05 laoqiren

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

paulobarcelos avatar May 15 '17 15:05 paulobarcelos

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 avatar May 17 '17 13:05 jpsc

@jpsc But this doens't work in css using styled-jsx.

laoqiren avatar May 17 '17 14:05 laoqiren

@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 avatar May 18 '17 22:05 arunoda

@arunoda yes, It doens't work just using background-image: url("/static/my-image.png").

laoqiren avatar May 19 '17 01:05 laoqiren

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

manishoo avatar Jul 02 '17 21:07 manishoo

@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 avatar Jul 02 '17 21:07 lededje

@lededje I want to use a webpack/babel loader to require images in my components.

e.g: <img src={require('./myImage.png')} />

manishoo avatar Jul 02 '17 21:07 manishoo

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

lededje avatar Jul 02 '17 21:07 lededje

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.

lededje avatar Jul 02 '17 21:07 lededje

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.

arunoda avatar Jul 03 '17 05:07 arunoda

add the following babel plugin: https://www.npmjs.com/package/babel-plugin-inline-import-data-uri

vzaidman avatar Feb 20 '18 15:02 vzaidman

You can actually use webpack loaders in v5, someone already made a plugin for handling images: https://github.com/arefaslani/next-images

timneutkens avatar Feb 20 '18 16:02 timneutkens

See also: https://github.com/cyrilwanner/next-optimized-images

tomsoderlund avatar Sep 14 '18 20:09 tomsoderlund

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()
});

tarang9211 avatar Oct 01 '18 15:10 tarang9211

Hi

defi-engineer avatar Nov 10 '18 23:11 defi-engineer

You can actually use webpack loaders in v5, someone already made a plugin for handling images: This is not working

defi-engineer avatar Nov 10 '18 23:11 defi-engineer

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.

AndrewRayCode avatar Nov 18 '18 23:11 AndrewRayCode