create-react-app: Compilation error with react-scripts 4.0.0 ( Can't resolve file )
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! xxxxxxxxxxxxxxx@1.0.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the xxxxxxxxxxxxx@1.0.0 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
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 103
- Comments: 71 (1 by maintainers)
Commits related to this issue
- Update libraries - hack https://github.com/facebook/create-react-app/issues/9870 — committed to nancy134/realty-app by nancy134 4 years ago
- fix(deps): workaround resolve issue See: https://github.com/facebook/create-react-app/issues/9870#issuecomment-774635475 — committed to technologiestiftung/berlin-iot-hub-frontend by dnsos 3 years ago
- fix: css-loader file resolution bug introduced by CRA v4 In recent commits, we upgraded our react-scripts version from 3.4.4 to 4.0.3. This is because CRA (create-react-app) v3 uses an outdated versi... — committed to isomerpages/isomercms-frontend by deleted user 3 years ago
- update css-loader configuration Use example from https://github.com/facebook/create-react-app/issues/9870#issuecomment-749765746 — committed to transcom/move.mil by carterjones 3 years ago
- Upgrading react-scripts - this remove error about browserlist - we have to move all the referenced css url resources to src/components see more https://github.com/facebook/create-react-app/issues/9870 — committed to Autodesk-Forge/forge-configurator-inventor by vasicem 3 years ago
- Downgrade CRA to 3.4.4 and explicitely resolve typescript-eslint Some breaking changes in CRA https://github.com/facebook/create-react-app/issues/9870 version 4.0.1 — committed to opensrp/miecd by peterMuriuki 3 years ago
- fix: css-loader file resolution bug introduced by CRA v4 In recent commits, we upgraded our react-scripts version from 3.4.4 to 4.0.3. This is because CRA (create-react-app) v3 uses an outdated versi... — committed to isomerpages/isomercms-frontend by deleted user 3 years ago
- Add linting and formatting tools (#378) * fix: outdated packages with vulnerabilities * feat: install eslint and initiate config * feat: install prettier and set prettier options * feat: ins... — committed to isomerpages/isomercms-frontend by kwajiehao 3 years ago
- Add linting and formatting tools (#378) * fix: outdated packages with vulnerabilities * feat: install eslint and initiate config * feat: install prettier and set prettier options * feat: ins... — committed to isomerpages/isomercms-frontend by kwajiehao 3 years ago
- (Merge to prod) 30 April (#436) * Add linting and formatting tools (#378) * fix: outdated packages with vulnerabilities * feat: install eslint and initiate config * feat: install prettier an... — committed to isomerpages/isomercms-frontend by gweiying 3 years ago
- fix: import jagged-blue-rect.svg - Cannot import from public folder, so move to src folder - Reorg components into folders https://github.com/facebook/create-react-app/issues/9870 — committed to jxjj/jjohnson.me by jxjj 2 years ago
I’m also seeing this issue where we have an image in
/public
but it’s not getting resolved when reference in css usingurl
:.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'
Having a similar issue but with sass
url()
imports of images in the/public
folderIf 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 changeto
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.
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'
Chill. Also, this is a major update. That implies breaking changes.
Can confirm this happens with
url()
calls in@font-face
directives as well as perreact-scripts@4.0.0
. For instance:Edit: I dug into the changelog and ~I’m a little confused as to why it stopped working since
css-loader
andpostcss-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 percss-loader@4
it becomes processed: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 ajsconfig.json
as described here: https://code.visualstudio.com/docs/languages/jsconfigThanks to chriswininger. I could resolve my issue following his advice.
I changed the syntax of quotes from url(‘/images/img-2.jpg’) to ‘url(/images/img-2.jpg)’ in my App.css file
As developer of
css-loader
, I want to say thatcss-loader
supports/images/image.png
inurl
, CRA should addsrc
toresolver.roots
(and using/images/image.png
will be resolved as/src/images/image.png
, it is default behavior for webpack@5)@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?
I also have a problem when loading fonts from public folder. It did work in 3.x.x
Just note, the latest version of
css-loader
supports/* webpackIgnore: true */
, i.e. you can writeAlso experiencing the same issue using react-scripts 4.0.1. The comments in https://github.com/webpack-contrib/css-loader/issues/1136 are less than helpful. I have an image in
<root>/public/assets
I am trying to reference in the css asand am getting
The options as I see it are:
src
and avoid the css-loader altogether. Would prefer not to as means changing the logical structure of my project just to workaround a problem with tooling./public/assets/image.jpg
. Fails with the error messageIs there any improvement on this, and what would be needed to be changed in create-react-app/react-scripts to get
url()
to just work with the<root>\public
directory in react apps?I ran into this when upgrading a large project to the latest react-scripts. All css url(‘/assets’) imports break due to absolute path changes in css-loader. My workaround isn’t ideal but it allows compilation without major refactors.
/public/assets
because any jsx needs eg.<img src="/assets/file.png" />
/src/assets
./src/assets
folderurl('/assets/file.png')
withurl('../../assets/file.png')
Looking at
css-loader
it looks like they’re not going to fix it https://github.com/webpack-contrib/css-loader/issues/1136#issuecomment-664984703You would have to include
/public
to the resolve paths. I just moved my assets to/src
as I didn’t want to ejectWhat 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 tosrc/
.Here’s a fix using
react-app-rewired
for css files with absolute links. Inconfig-override.js
, do:For those used to
patch-package
, here’s the patch based on @elylucas workaroundEdit: 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)To be fair, there was a warning in v3.4.4
But this does not solve the style issues using
url()
referencing images frompublic
folderLooks like rearranging the structure of the project is the recommended way to go then. The separation of content between src and public is a bit confusing then (to me) - why have a public folder at all as part of CRA if its criteria is any static assets unless you reference them in the code. I get that CRA is opinionated, and that’s fine - this particular opinion can get confusing…
(In the meantime I’ve converted the projects to webpack 5 anyway 😉 )
Our product needs more nuanced resolution. Setting
url: false
will then prevent relative file paths from resolving. Best of both worlds may be adding conditional resolution, something like:When compiled,
/example.jpg
will still be/example.jpg
but./local.jpg
will be<dynamic_path>/local.<hash>.jpg
.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
I’d suggest there is a difference between fix and workaround. A fix would be the tooling not inhibiting the dev from using the existing css
url()
feature inside a css file (as opposed to inline style attributes), and being able to reference assets where many (not all of course) would think is the most logical place (thepublic
folder). The workarounds all share one common feature - ways to switch off or bypass the newurl()
checks fromcss-loader
, which is not ideal, and also all have some level of hack around what a dev would normally do here to get an image/font/whatever referenced from css. Currently, all we have are workarounds, so I guess that’s what we make do with for now 😃I’m surprised that more people haven’t realised that this is a major concern for SEO.
Lets say that you have a image file within the src folder, and you’re using it within your CSS file like this -
.Profile { text-align: center; background-image: url(“…/…/images/user/profile.png”); }
If this profile.png file is inside the
src > images > user
directory, then while creating a build with react-scripts, the URL generated for this file will be something like this -https://{my-domain.com}/static/media/profile.{hash}.jpg
This {hash} value changes with every build. Therefore, if you use this in a static website, and if your website is crawled by google bot, you may see 404’s reported in the google search console the next time you deploy, because Google can take several weeks before indexing your site again.
This is where an image in the
public
folder would have helped, because that URL will never change, and can safely remain indexed in search engines.The public folder was supposed to serve files during development which
Is this not the case anymore?
@donal-s Yes, just add
public
toresolver.roots
Anyway we can implement:
and don’t touch this url, if somebody want this, feel free to open a new issue in
css-loader
Please update webpack to the latest version (v4)
Great 👯 It’s worked.
react-scripts
5.0.0 was released and this issue was not addressed.https://github.com/facebook/create-react-app/releases/tag/v5.0.0
Some excerpts from the official docs related with this issue:
Using the Public Folder | Create React App
Adding Images, Fonts, and Files | Create React App
I used the “patch” approach in some projects for a while but finally switched to the relative path approach (by moving images into
src/
directory).I followed @Gatix comment:
Moving assets (at least those read by css-loader) from
public
tosrc
is not a big deal and is the “recommended way” of Adding Images, Fonts, and Files as stated in the CRA documentation: https://create-react-app.dev/docs/adding-images-fonts-and-filesFor example, when you import font files from
src
, they are seemlessly copied in the production build:Note that the hash part (e.g.
504a4f4a
), included in the built files name, seems to be based on the file content, so it shouldn’t break assets caching after each build deployment.I had a scan of webpack, css-loader, and react-scripts. I’m still not clear on what exactly needs to be changed where in react-scripts to fix this issue. Could someone more familiar with these projects take a look at applying @alexander-akait 's suggestion please?
Would adding
public
toresolver.roots
mean that addingurl(/assets/image.png)
correctly resolves to<rootdir>/public/assets/image.png
? I think that’s what we want.I am no expert but I have an easy fix to this css image issue. I used react style attribute (inline) for the image path. and did the rest of the styling in css. That way you can still support styling like background-attachment: fixed and so forth
@donal-s
Or option 5, see @atiastudio comment. Change any css url imports to jsx
Hello, guys, I’ve also faced this issue after updating to 4.0.0 (or actually 4.0.1).
As a workaround I use declaring background image right in JSX, like you know:
<div style={{background: 'url(/img/bg/image.jpg)'}}><div>
It works, because obviously css-loader has nothing to do with JSX files.
And it works for me, because code is arranged in a way where background can be changed depending on the section of application. In other words I use 1 component with several times with different images or maybe in different projects.
As for other projects, I’d like to join collective resentment. It seems to be strange to store images in src and I certainly don’t have a teeny tiny intention to mess with webpack 😃
I can’t believe this is still an issue. Setting
url: false
will simply disable resolving anything from the public directory.This will solve the issue given the following folder structure:
yarn add -D patch-package
node_modules
folder navigate to/node_modules/react-scripts/config/webpack.config.js
and find lineoptions: cssOptions
and replace it withoptions: {...cssOptions, url: url => !url.startsWith("/resources")},
(This will ensure that any import which starts with/resources
will get resolved from the public directory.)yarn patch-package react-scripts
. This will create a new directory calledpatches
which should contain a git-patch for the react-scripts package.package.json
add the following to the scripts block:This will run the patches after every install. You can (and should) commit these files to the repository.
Thank you! Finally something workable. You can also add a style section to the index.html which is guaranteed to bypass all the transpilation.
This is very annoying issue
To the above list of workarounds, I can add the following pragmatic one if you are facing this error while trying to load local fonts from the
public
folder:Add (for the time being) the style in your
index.html
.It works, thanks!
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:
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 usingwebpack/css-loader@3
but needed to be changed like displayed below to avoid issues when usingwebpack/css-loader@4
./src/app/styles/rsuite/index.less
: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: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:and had to roll back like this
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).this is crazy! no testing seriously? I broke a production app before deploying thank God!
you have not even mentioned this anywhere??
This solution solved the issue for me. I think this is the best way.
I wanted to load a background image from Tailwind, but I got the error “./src/index.css Error: Can’t resolve '/img…”, CRA comes with “@craco/craco”: “^6.4.0”, and “react-scripts”: “4.0.3”, for these versions, it doesn’t work to load the image with the relative path. Performing @ejose19’s workaround works but it is not ideal. My solution was to downgrade the versions of “@craco/craco”:“^5.6.0” and “react-scripts”:“^3.3.0-next.38” and it worked without problems. What a way to waste time…
@mghazanfar this will disable all url resolving.
We wanted only to not resolve urls from
/resources
url so the following worked for usThat way other urls that you use inside your scss files in components will still resolve.
Worked for my finally after 3 days long struggle. Thanks
Who dis new phone?
I am the engine.