Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Upgrade From 5 to 6: asset path must start with the project source root

I upgraded my existing app from Angular 5 to 6.

After i run ng serve i get the following error:

The node_modules/font-awesome/fonts asset path must start with the project source root. Error: The node_modules/font-awesome/fonts asset path must start with the project source root. at assetPatterns.map.assetPattern (/home/kay/Documents/<>/<>/node_modules/@angular-devkit/build-angular/src/utils/normalize-asset-patterns.js:37:23)

As you can see below i have an assets array that pulls in fonts from the node module package. This was not an issue in Angular 5.

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "demo": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.app.json",
            "polyfills": "src/polyfills.ts",
            "assets": [
              "src/assets",
              "src/favicon.ico",
              "../node_modules/font-awesome/fonts"
            ],
            "styles": [
              "src/styles.scss",
              "src/theme.scss",
              "node_modules/font-awesome/css/font-awesome.min.css"
            ],
            "scripts": [
              "node_modules/hammerjs/hammer.min.js",
              "node_modules/auth0-js/build/auth0.min.js",
              "node_modules/moment/min/moment.min.js",
              "node_modules/ua-parser-js/dist/ua-parser.min.js",
              "node_modules/d3/dist/d3.min.js",
              "node_modules/wordcloud/src/wordcloud2.js",
              "node_modules/chart.js/dist/Chart.bundle.min.js",
              "node_modules/progressbar.js/dist/progressbar.min.js"
            ]
          },
          "configurations": {
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "demo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "demo:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "demo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "karmaConfig": "./karma.conf.js",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "scripts": [
              "node_modules/hammerjs/hammer.min.js",
              "node_modules/auth0-js/build/auth0.min.js",
              "node_modules/moment/min/moment.min.js",
              "node_modules/ua-parser-js/dist/ua-parser.min.js",
              "node_modules/d3/dist/d3.min.js",
              "node_modules/wordcloud/src/wordcloud2.js",
              "node_modules/chart.js/dist/Chart.bundle.min.js",
              "node_modules/progressbar.js/dist/progressbar.min.js"
            ],
            "styles": [
              "src/styles.scss",
              "src/theme.scss",
              "node_modules/font-awesome/css/font-awesome.min.css"
            ],
            "assets": [
              "src/assets",
              "src/favicon.ico",
              "node_modules/font-awesome/fonts"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "demo-e2e": {
      "root": "",
      "sourceRoot": "",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "./protractor.conf.js",
            "devServerTarget": "demo:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "e2e/tsconfig.e2e.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "demo",
  "schematics": {
    "@schematics/angular:component": {
      "prefix": "app",
      "styleext": "scss"
    },
    "@schematics/angular:directive": {
      "prefix": "app"
    }
  }
}
like image 200
Kay Avatar asked May 06 '18 18:05

Kay


3 Answers

Ok, Clarifying Abann's solution. You must use the long form for assets outside the sourceRoot. I do think the documentation could be much more clear on that. It states

You can use this extended configuration to copy assets from outside your project. For instance, you can copy assets from a node package:

But really, it should say you must use

"assets": [
          "src/assets",
          "src/favicon.ico",
           {
            "glob": "*",
            "input": "node_modules/font-awesome/fonts",
            "output": "assets/fonts/"
          }
        ],

Where assets/fonts/ is the path you probably configured in your SASS/SCSS file's $fa-font-path variable override.

like image 155
Eric Liprandi Avatar answered Nov 08 '22 23:11

Eric Liprandi


Edit 1

Okay try this:

"assets": [
    {"glob": "**/*", "input": "./node_modules/youpackage", "output": "/where_you_want_theasset/"}
]

reference: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/asset-configuration.md

Original answer: since it helped me(all my assets are in src)

I got the same error after updating,

try adding the root of your app to the angular.json

for me the sourceRoot and root are the same

"root": "src",
  "sourceRoot": "src",

like so:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "demo": {
      "root": "src",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
like image 14
abann sunny Avatar answered Nov 08 '22 23:11

abann sunny


changing sourceRoot to '.' seems to have resolved this and allowed me to compile, and a superficial test shows everything working. not confident that this won't introduce other unintended consequences though.

like image 3
Brian Hadley Avatar answered Nov 09 '22 01:11

Brian Hadley