0

I tried to run index.html inside dist folder in the browser but its not working, unlike the angularjs application where we import the script file in the index.html and the application simply works.

Why we can't do that in the Angular Project as there are some javascript files which are imported in the index.html same as any other javascript project.

shyamzzp
  • 115
  • 7
Swaroop
  • 7
  • 2
  • angular does not run on `file` protocol. Read this related [issue](https://github.com/angular/angular/issues/13948#issuecomment-302727428). – Reigel Gallarde Aug 02 '19 at 04:29
  • This question has already been asked. Below link can answer your question https://stackoverflow.com/questions/34580772/why-do-i-need-a-http-server-to-run-angular-2 – Ashish Sharma Aug 02 '19 at 04:59

2 Answers2

4

You can do that in Angular application as well. You need to install http-server for that.

Follow the below steps-

  1. Install http-server from npm server using following command-

    npm install http-server -g

  2. Generate the build in dist folder using command-

    ng build --prod --aot --output-hashing=none

  3. Run the command to run the project from dist folder-

    run http-server ./dist

This will start serving your project from dist folder.

Neeraj Shende
  • 310
  • 1
  • 7
0

When index.html is being loaded directly in the browser, it doesn't use the http protocol. It is being loaded over the local file system over file:/// Browsers don't allow direct access to the .js files from a file system because of the security reasons.

The file system is not providing needed features of a server which is required. Github comment with some more details.

enter image description here

At a minimum, a simple static HTML server is required to run the Angular applications.

Angular apps are perfect candidates for serving with a simple static HTML server. You don't need a server-side engine to dynamically compose application pages because Angular does that on the client-side. Read More

Lahar Shah
  • 7,032
  • 4
  • 31
  • 39