3

How can I use Angular 2 in production without webserver and just the filesystem? I can't use a webserver because of the corporate policies (that was not a problem with Angular because I could use the filesystem).

4
  • Do you want to use the app over the network? Commented Apr 21, 2017 at 7:55
  • What have you tried and where are you stuck? Commented Apr 21, 2017 at 8:09
  • what are your use cases? xhr doesn't work in file:/// protocol Commented Apr 21, 2017 at 8:14
  • I have this error : "XMLHttpRequest cannot load file:///[...]/src/main.js. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." Indeed, I have this error because I don't use a webserver (when I use one, there is no problem). The question is how to avoid this problem Commented Apr 21, 2017 at 8:41

1 Answer 1

1

If you don't need network, Angular (version 2 and higher) applications work with no problems over the file:/ protocol. But you must set a correct base href path in your index.html. If you are using Angular CLI, you can build the app using the following command:

ng build --base-href=/path/to/your/application/dist/

and then opening the file:///path/to/your/application/dist/index.html in your browser.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. I have the following error when using your command : "Cannot read property 'config' of null". Also I don't have a "dist" folder, just a src and a src/app folder...
I don't know what could cause the problem with a null reference. The dist folder should be created when you run the ng build command and it will contain the built application.
I tried to clone another project and used the ng build then opened the index.html file in the dist folder and it worked perfectly :) ! Thanks for you help Ján!
That's good to hear. You may also want to use the HashLocationStrategy so when you reload the page in a browser, it still loads the application (index.html): angular.io/docs/ts/latest/guide/router.html#!#location-strategy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.