next.js: SCSS modules cannot load images correctly with `next-images` package
Update: Read my other comments below for a more pinpointed problem.
Bug report
Describe the bug
I tried to set a background image in a SCSS module with background: url("./footer-region-bg.jpg") repeat; (is in the same directory as the component). But this results in the following when fetching the image.

This was working with @zeit/next-sass, but it doesn’t with the native scss modules.
To Reproduce
Load an Image from the same directory as the component in an SCSS module like mentioned above. Use the native SCSS modules from NextJs.
Expected behavior
The image should be loaded correctly
System information
- OS: Windows
- Version of Next.js: 9.3.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 17 (5 by maintainers)
Commits related to this issue
- Apply workaround for NextJs bug zeit/next.js#11164 — committed to aantthony/next-optimized-images by aantthony 7 years ago
I’m not sure if it is a
next-imagesissue or the Next.js itself.I faced the same issue after upgrading from
9.1.7to9.3.1. I was usingnext-sassandurl-loaderwith the custom webpack config.By using the built-in Sass feature, background and font face
url()s inside.scssfiles stop working. After digging into Next.js files I realized that it handlesurl()imports through thefile-loadernow.https://github.com/zeit/next.js/blob/v9.3.1/packages/next/build/webpack/config/blocks/css/index.ts#L278-L292
https://github.com/zeit/next.js/blob/v9.3.1/packages/next/build/webpack/config/blocks/css/index.ts#L17
I hadn’t used the built-in CSS feature before and that is why I hadn’t faced the issue.
Workaround
I ended up with excluding the
url()file imports by setting a test regex in webpackissuerconfig as follows:Hope this helps.
Solved!
style.module.scss
next.config.js
Thanks guys for reporting this issue. I’ll fix that in the
next-imagespackage today and will release a new patch for that.The “fix” doesn’t solve importing images into js files through template literals.
Given the
src/styles/index.module.cssfile:Example 1
no
next.config.jssrc/pages/index.js:Example 2
next.config.js:src/pages/index.js:Example 3
next.config.js:src/pages/index.js:So what should I do to get all three
this workstogether ?Same here. When I remove my
url-loaderrelated code innext.config.js,background-imageworks fine. There’s my code :The “fix” here appears to be mostly a workaround though. It would be nice if Next detected the use of image processing plugins like
next-imagesornext-optimized-images, particularly the latter. Right now there’s a working workaround fornext-optimized-imagesbut it doesn’t allow you to run images imported in SCSS through i.e.optipngetc.This might still be an upstream issue (some way to tie into Next’s handling of images to apply optimizations?) but it seems like there’s precedent here for the “detect and disable” functionality for handling images since that’s what happens right now i.e. if you’re using
next-sass. From the official 9.3 blog post:I realize this issue mentions
next-imagesspecifically in the title, let me know if I should open a new issue instead.@theshem Can you open a new issue proposing to add support for data URIs OOTB?
One thing to note, using data URIs are not generally a safe default because it’s incompatible with SVG sprite systems, for example.
If we were to use data URIs out of the box (for small assets), SVGs would need to be excluded: https://css-tricks.com/svg-fragment-identifiers-work/
Alright, after some more testing I found something interesting. The problem can be reproduced when using the
next-imagespackage. My currentnext.config.jslooks like this:For some reason this config breaks the SCSS/CSS module images resolver.
Update: The same is true for the
https://www.npmjs.com/package/next-optimized-imagespackage.