17

I'm developing an angular6 application and I want to generate a build to test on my server, currently I use ng server and it runs working on my browser without generating any errors.

c:\Users\emiry\Desktop\Angular\Projects\StartingNewProject

when I run the ng build command it generates a build for the /dist folder and returns no errors

on my machine I have installed wamp64 so I get this build generated by ng and put it in my www folder

C:\wamp64\www\StartingNewProject

Just this would be enough for me to be able to run my application on the server?

when I try to access my application that is on the server through the http:// localhost/startingnewproject/

it returns me the following error in the browser console

Failed to load resource: the server responded with a status of 404 (Not Found) polyfills.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) styles.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) vendor.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) main.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "StartingNewProject": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/StartingNewProject",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css",
              {
                "input": "./node_modules/bootstrap/dist/css/bootstrap.css"
              }
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "StartingNewProject:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "StartingNewProject:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "StartingNewProject:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "StartingNewProject-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "StartingNewProject:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "StartingNewProject:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "StartingNewProject"
}

package.json

{
  "name": "starting-new-project",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "@ng-bootstrap/ng-bootstrap": "^2.0.0-alpha.0",
    "@ng-bootstrap/schematics": "^2.0.0-alpha.1",
    "bootstrap": "^4.0.0",
    "core-js": "^2.5.4",
    "jshint": "^2.9.5",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^6.0.3",
    "@angular-devkit/build-angular": "~0.6.8",
    "typescript": "~2.7.2",
    "@angular/cli": "~6.0.8",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  }
}

I tried to access as follows

file:///C:/wamp64/www/StartingNewProject/index.html

5index.html:13 GET file:///C:/runtime.js 0 () /C:/favicon.ico:1 GET file:///C:/favicon.ico 0 () translate.google.com/gen204?nca=te_li&client=te_lib&logld=vTE_20180625_00:1 GET file://translate.google.com/gen204?nca=te_li&client=te_lib&logld=vTE_20180625_00 0 ()

file:///C:/wamp64/www/StartingNewProject/index.html

GET file:///C:/runtime.js 0 () /C:/favicon.ico:1 GET file:///C:/favicon.ico 0 ()

When I try to access the root directory to see the folders and files, I can perfectly, the problem is when I load the index.html

Emiry Mirella
  • 567
  • 1
  • 7
  • 21
  • If you paste `C:\wamp64\www\StartingNewProject` or `C:\wamp64\www\StartingNewProject\index.html` into your browser, does that load the application? – user184994 Jul 31 '18 at 20:01
  • Hi, I tried to access and I had some problems, I put the results I got in the question – Emiry Mirella Jul 31 '18 at 20:07
  • Okay, so it looks like it can at least find the file, which is good. Have you checked that your wamp64 service is running and that it is configured with apache correctly? – user184994 Jul 31 '18 at 20:09
  • yes, in my wamp I also have my api to connect with this my angular project and when I execute the ng serves in angular I can access the api correctly – Emiry Mirella Jul 31 '18 at 20:11
  • If you just browse to `http://localhost`, does the WampServer homepage show? (not your Angular Homepage) – user184994 Jul 31 '18 at 20:15
  • yes, the page is displayed normally and in Your Projects the StartingNewProject appears – Emiry Mirella Jul 31 '18 at 20:16
  • Sorry, I misunderstood your question. I thought the 404 was for the entire Angular app, but it's not. It's just for the relative paths, which is due to the `base` tag. I've added an answer below for you – user184994 Jul 31 '18 at 20:25
  • Okay, I'll try to execute the method you reported. – Emiry Mirella Jul 31 '18 at 20:27

3 Answers3

35

The issue that the project is having is in trying to find it's dependencies. The index.html file of your Angular project should contain a tag that looks like:

<base href="/">

Or similar. That means, when it looks for dependencies, it looks for the relative to the root. In your case,there not next to the root (i.e. localhost), they're under StartingNewProject

You can should generate a new build with ng build --prod --base-href=/StartingNewProject/ which will add the base href tag for you. Please note that it is important to include both the leading and trailing slash here.

More details can be found in the Angular docs, here

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
user184994
  • 17,791
  • 1
  • 46
  • 52
  • I'm sorry but I did not understand where do I find this – Emiry Mirella Jul 31 '18 at 20:29
  • Yep, you can run that build command without needing to change the HTML manually – user184994 Jul 31 '18 at 20:31
  • I run the ng build command -base-href = / StartingNewProject and when I go to http: // localhost / startingnewproject / it returns me GET http: //localhost/runtime.js 404 (Not Found) – Emiry Mirella Jul 31 '18 at 20:35
  • If you manually open `C:/wamp64/www/StartingNewProject/index.html`, can you see the `` html tag? Is the `href` attribute set to `"/StartingNewProject"`? – user184994 Jul 31 '18 at 20:38
  • I just manually add the index.html file and the error continues – Emiry Mirella Jul 31 '18 at 20:38
  • when I created my project I added the --routing and also added the bootstrap scheme, does this have any problems? – Emiry Mirella Jul 31 '18 at 20:40
  • You added it to the one in `C:/wamp64/www/StartingNewProject/index.html`, yes? And there shouldn't be any space in there. Make sure you remove the space. And no, there won't be any issue with either of those things. – user184994 Jul 31 '18 at 20:41
  • I created a new build of my project and put it in the folder C: / wamp64 / www /. the tag is – Emiry Mirella Jul 31 '18 at 20:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177133/discussion-between-user184994-and-emiry-mirella). – user184994 Jul 31 '18 at 20:43
  • for me, I remove the tag in index.html file and insert {provide: APP_BASE_URL, useValue: '/'} in provider property of @NgModule in AppModule.js file and tried to build the project to make it work. – aj go Mar 19 '19 at 22:09
  • I have the same issue, but already added base tag with and still got 404 cause I think the scripts src should also contain the relative path, something like this: how can I solve this? – LeonardoX May 03 '19 at 00:03
11

I was facing the same problem. I fixed this using ./.

<base href="/"> to <base href="./">

again build and deploy.

Saad Abbasi
  • 745
  • 11
  • 25
1

you may get a "deprecated" warning with Angular 11 when using

ng build --prod --base-href=/StartingNewProject/

You can instead adjust angular.json.

In angular.json file, create a "baseHref" property like this:

 "projects": {
   "your-project-name" : {
          "architect": {
             "build": {
               "options": {
                   "baseHref": "/your-relative-path/"

This will change the URL both in production and development.

Beware that, in your links, always use relative paths like this (instea of "/"):

 <img class="top-bar__logo" src="./assets/images/picture1.svg" />
Atilla Baspinar
  • 925
  • 1
  • 12
  • 17