Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular browser-esbuild error: Could not resolve absolute path [plugin angular-css-resource]

During migrating from angular v15 to v16 in a nx environment, my build process encounters with lots of errors regarding to my form of addressing assets in scss files.

For example:

✘ [ERROR] Could not resolve "~apps/platform/src/assets/img/bank-card-overlay.png" [plugin angular-css-resource]

    apps/platform/src/app/shared/components/add-credit-card-modal/add-credit-card-modal.component.scss:40:24:
      40 │ ...image: url("~apps/platform/src/assets/img/bank-card-overlay.png");
         ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can remove the tilde and use a relative path to reference it, which should remove this error.
  The plugin "angular-compiler" was triggered by this import

    apps/platform/src/app/shared/components/add-credit-card-modal/add-credit-card-modal.component.ts:3:33:
      3 │ ...CE__1 from "angular:jit:style:file;./add-credit-card-modal.compo...
        ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I didn't try using relative path because I need to do it the way it is regarding to maintain development experience.

Any solutions?

like image 866
MahdiJoon Avatar asked May 29 '26 20:05

MahdiJoon


2 Answers

What solved my issue is adding the following to angular.json:

"options": {
  "stylePreprocessorOptions": {
    "includePaths": [""]
  },
}

like image 166
vlio20 Avatar answered May 31 '26 09:05

vlio20


Ensure you have your assets set up correctly in angular.json:

"assets": [
     "src/assets"
 ],

Now your assets folder should be in src folder in your project. If you have an image in your assets folder named Solar.jpg then you will be able to reference this image in scss by the following code:

in app.component.scss:

.myImage {
    height: 200px;
    background-image: url('/assets/Solar.jpg');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

Note with scss files you have to have a '/' before assets. But if you reference an asset in your .html files then it should be without the '/', e.g. "assets/Solar.jpg". Don't understand why Angular made it this way.

like image 45
Franco Avatar answered May 31 '26 11:05

Franco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!