latest update of react-scripts to 4.0.0 from 3.4.4 causes a compilation problem.
With 3.4.4 it works, and the file exists
./src/styles/mycssfile.css
Error: Can't resolve '/images/module/filename.png' in 'D:\dev\workspace\xxxxxxxxx\src\styles'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\xxxxxxxxxxxxxx\AppData\Roaming\npm-cache\_logs\2020-10-23T16_16_47_530Z-debug.log
Having a similar issue but with sass url()
imports of images in the /public
folder
What does the import line look like? What kind of file is it in?
I had the same url()
issue as @efeichen and fixed it by moving images to src/
.
@jrr background-image: url(/contact-clip.svg);
it's in a sass file in /src/scss
I'd like to move the images to src/
since I believe the relative path from the sass file to the image is working (but can't do that for images in public
because of the can't import from outside src restriction), but I'd prefer not to unless absolutely necessary (I have a fairly large public folder). Thanks for the tip though!
I have the same problem with SCSS and CSS files (images and fonts). In 3.x.x works but now it says it can't resolve the file in src.
I also have a problem when loading fonts from public folder. It did work in 3.x.x
I had the exact same issue, it started just today when I upgraded to 4.0.0
src: url(/fonts/josefin-sans-regular.ttf)
And it leads to the error:
Error: Can't resolve '../fonts/josefin-sans-regular.ttf' in '.\frontend\src\scss'
I had this error on a project that was using absolute imports. It seems like setting NODE_PATH=src/
in the .env
file is no longer supported. I instead had to use a jsconfig.json
as described here: https://code.visualstudio.com/docs/languages/jsconfig
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
I'm also seeing this issue where we have an image in /public
but it's not getting resolved when reference in css using url
:
.background-image {
background: url('/bg.jpg');
}
Error:
./src/styles/style.css (./node_modules/css-loader/dist/cjs.js??ref--4-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/styles/style.css)
Error: Can't resolve '/bg.jpg' in '/mnt/c/git/application/src/styles'
Can confirm this happens with url()
calls in @font-face
directives as well as per [email protected]
. For instance:
@font-face {
font-family: 'Nunito';
font-style: normal;
font-weight: normal;
font-display: swap;
src: local('Nunito Bold'), local('Nunito-Bold'), url('/fonts/nunito.woff2') format('woff2');
}
./src/index.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/index.css)
Error: Can't resolve '/fonts/nunito.woff2' in '/Path/To/Project/src'
Edit: I dug into the changelog and ~I鈥檓 a little confused as to why it stopped working since css-loader
and postcss-loader
were not updated~ css-loader
was in fact updated.
It looks like this is caused by the css-loader
migration to v4: https://github.com/webpack-contrib/css-loader/commit/bc19ddd8779dafbc2a420870a3cb841041ce9c7c#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L128.
Originally, a root-relative path like /foo/bar.ext
would remain untouched, but as per css-loader@4
it becomes processed:
To be fair, there was a warning in v3.4.4
But this does not solve the style issues using url()
referencing images from public
folder
We are using another repo folder for the assets. I'm getting an error:
./src/components/HTMLPreview/HTMLPreview.module.scss (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-7-1!./node_modules/postcss-loader/src??postcss!./node_modules/resolve-url-loader??ref--5-oneOf-7-3!./node_modules/sass-loader/dist/cjs.js??ref--5-oneOf-7-4!./src/components/HTMLPreview/HTMLPreview.module.scss)
Error: Can't resolve '../../assets/iphone.svg' in '.../src/components/HTMLPreview'
How can I disable this validation and use this path?
Worked in v3.4.4
this is crazy! no testing seriously? I broke a production app before deploying thank God!
you have not even mentioned this anywhere??
this is crazy! no testing seriously? I broke a production app before deploying thank God!
you have not even mentioned this anywhere??
@x5engine What? Who is "you"?
Edit:
The 馃憥 urges me to clarify that the question _Who is "you"?_ could be replaced with _Who has not even mentioned this anywhere?_
this is crazy! no testing seriously? I broke a production app before deploying thank God!
Chill. Also, this is a major update. That implies breaking changes.
this is crazy! no testing seriously? I broke a production app before deploying thank God!
you have not even mentioned this anywhere??
@x5engine What? Who is "you"?
Who dis new phone?
I am the engine.
this is crazy! no testing seriously? I broke a production app before deploying thank God!
Chill. Also, this is a major update. That implies breaking changes.
Yeah man I was going nuts.
Hope this gets sorted out soon 馃檪
running the same issue... waiting for clues
If you care to dive into the react-scripts webpack setup, you can pass in the option to disable url routing to css-loader. In node_modules/react-scripts/config/webpack.config.js
on line 121 change
options: cssOptions,
to
options: {...cssOptions, url: false},
Of course this entails all the issues that come around with modifying the config inside of node_modules, but hopefully this might help in a long term fix for the change in css-loader resolving the urls.
Looking at css-loader
it looks like they're not going to fix it https://github.com/webpack-contrib/css-loader/issues/1136#issuecomment-664984703
You would have to include /public
to the resolve paths. I just moved my assets to /src
as I didn't want to eject
For those used to patch-package
, here's the patch based on @elylucas workaround
diff --git a/node_modules/react-scripts/config/webpack.config.js b/node_modules/react-scripts/config/webpack.config.js
index 80c6ac2..01de68e 100644
--- a/node_modules/react-scripts/config/webpack.config.js
+++ b/node_modules/react-scripts/config/webpack.config.js
@@ -117,7 +117,7 @@ module.exports = function (webpackEnv) {
},
{
loader: require.resolve('css-loader'),
- options: cssOptions,
+ options: {...cssOptions, url: false},
},
{
// Options for PostCSS as we reference these options twice
Edit: Actually, that workaround only solves the issue in development (npm start
), but build files are using a wrong import (they're being prefixed with ../
making it fail)
I had an issue loading fonts from within CSS. But thanks to @evilebottnawi pointing out how webpack/css-loader@4+ resolves URLs i was able to fix my issues:
/font/font.woff2 never was and never can be ./font/font.woff2, please read spec about URL resolving in CSS
I use rsuite as a UI framework. To override default icon font settings you can set a less
variable @icon-font-path
to suit your needs. I did that in a way which worked when using webpack/css-loader@3
but needed to be changed like displayed below to avoid issues when using webpack/css-loader@4
.
/src/app/styles/rsuite/index.less
:
- @import '../../../../node_modules/rsuite/lib/styles/themes/default/core';
+ @import './node_modules/rsuite/lib/styles/themes/default/core';
- @icon-font-path: '/fonts/rsuite-icon';
+ @icon-font-path: './fonts/rsuite-icon';
My icon font is located in /public/fonts/rsuite-icon
.
Maybe this helps by getting the idea of how URLs are resolved.
Edit:
Forgot to mention how @icon-font-path
is actually used:
@font-face {
font-family: @font-family-icon;
src: url('@{icon-font-path}/rsuite-icon-font.ttf') format('truetype'),
url('@{icon-font-path}/rsuite-icon-font.woff') format('woff');
font-weight: normal;
font-style: normal;
}
Edit:
Oh my, what was i thinking... I wasn't aware of a faulty configuration because of working fallback mechanisms i didn't notice. I had to update my craco.config.js
like this:
module.exports = function ({ env }) {
return {
style: {
css: {
loaderOptions: {
url: false,
},
},
},
...
};
};
and had to roll back like this
+ @icon-font-path: '/fonts/rsuite-icon';
- @icon-font-path: './fonts/rsuite-icon';
This way URLs in CSS won't be handled by webpack and you can use them like you would in regular CSS & HTML, if i got that straight.
If you do not rely on webpack handling URLs in CSS this could be an option for you.
I had to remove the .
from @icon-font-path: './fonts/rsuite-icon';
because otherwise the file URL would depend on the current URL (CSS works at https://domain.tld but doesn't work at https://domain.tld/path).
Most helpful comment
Having a similar issue but with sass
url()
imports of images in the/public
folder