49

I'm using this angular 4 seed app: https://github.com/2sic/app-tutorial-angular4-hello-dnn

It uses webpack and works fine.

It only seems to serve the dev files and not the dist/ folder.

I want to ng serve the dist folder.

Not sure the command to do this or if I need to install a lite server or something.

I run this command to create the dist folder (which works fine):

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

Now I want to run this build in the browser.

Willi Mentzel
  • 21,499
  • 16
  • 88
  • 101
AngularM
  • 13,932
  • 27
  • 78
  • 151
  • 1
    I am new to angular4, may I know what is dist, build – Soumya Gangamwar Apr 13 '17 at 10:18
  • 1
    @SoumyaGangamwar if you want to build for an environment, which should be deployed and accessible from outside `localhost`, you can put your compiled sources into a dist (distribution) folder - which is then served by a http-server and propagated to the "outside" world. and `dist` is just a default name widely used for that purpose... – Guntram Sep 14 '17 at 09:02
  • Below stack url can be helpful https://stackoverflow.com/a/53300931/7165958 – Shridhar Sagari Nov 14 '18 at 14:27
  • @Guntram: That's exactly what I would like to do (making it accessible outside localhost). How would I specify IP and port? – Cleb Feb 10 '19 at 18:14
  • `node_modules\.bin\ng serve --port 4200 --base-href /my-app/ --host 0.0.0.0` does a standard serve, and host 0000 makes the served app accessible to the network from another computer. (the ip is the ip of your computer) – Guntram Feb 11 '19 at 10:37

9 Answers9

64

You can use http-server for doing so. First of all generate a build using the command ng build --prod --aot --output-hashing=none. This will create a dist folder in your directory structure.

After this, run http-server ./dist, which will start serving your project from dist folder.

Make sure you have installed http-server globally using

npm install http-server -g

For reference, see https://www.npmjs.com/package/http-server

Noor Ahmed
  • 1,357
  • 7
  • 14
  • Thanks, been looking to test my application build. I knew I probably needed to run the build on a server but could not find info on this anywhere. This needs to be pushed higher up google ranking. – Madmenyo Dec 29 '17 at 04:32
29

At least for Angular apps, angular-http-server seems to be a nicer option.

First install it with your prefered package manager, say

npm install angular-http-server -g

or

yarn global add angular-http-server

Then execute it:

angular-http-server --path path/to/dist/folder

Look at the repo for more information about usage.

PS: According the author, it should also work with other SPA frameworks (React, Vue and so forth).

PPS: Do not use angular-http-server for production, use this solution for testing purposes only.

rsenna
  • 10,960
  • 1
  • 50
  • 60
  • 2
    Using the `angular-http-server` server solved the issue I had which I described at `https://stackoverflow.com/questions/52786539/all-requests-are-not-found-404-when-the-application-runs-on-an-external-server/52786726` – Stephane Oct 13 '18 at 09:51
  • @Stephane I'm glad it helped! – rsenna Oct 16 '18 at 14:40
  • 1
    How would I specify the IP address, e.g. `http://0.0.0.0`; port can be done via the `-p` flag. – Cleb Feb 10 '19 at 18:11
  • Note: I wondered what the `angular-http-server` vs `http-server` rule was. https://www.npmjs.com/package/angular-http-server says that `angular-http-server` should not be used as a production server; only for development; whilst https://www.npmjs.com/package/http-server claims `http-server` can be used for production. `nginx` can also be used (see https://medium.com/bb-tutorials-and-thoughts/how-to-serve-angular-application-with-nginx-and-docker-3af45be5b854) – JohnLBevan Jun 04 '20 at 20:31
  • 1
    @JohnLBevan Good point. When I posted this answer I assumed OP wanted a solution for development only - as a comment to another answer below OP says "I want to **test** the dist folder". Like you said, the procedure I describe here should **not** be used for production. – rsenna Jun 08 '20 at 07:16
17

You need a server to serve your generated build.

I am using http-server. Install http-server using:

npm install -g http-server

now go inside your dist folder and run this command

http-server

as shown here:

enter image description here

Check http://localhost:8080 in your browser

Deepak Kumar
  • 1,397
  • 1
  • 13
  • 31
3

A little tips

so you avoid to install globally

install in your root

npm i http-server

in your package.json

"scripts": {
    "pwa": "http-server ./dist"
  }

than

npm run pwa 
Whisher
  • 25,656
  • 28
  • 106
  • 180
3

I serve the dist folder with the Angular CLI...

ng serve --prod=true

When true, sets the build configuration to the production target. All builds make use of bundling and limited tree-shaking. A production build also runs limited dead code elimination.

https://angular.io/cli/serve

Bill
  • 339
  • 3
  • 5
3

I use the VS Code extension Live Server.

  1. Run your ng build which will output the dist/ folder with index.html
  2. Open separate instance of VS Code onto the dist/ folder
  3. Press "Go Live" button in bottom right of VS Code status bar enter image description here which you will see after installing the Live Server extension.
H Dog
  • 3,068
  • 29
  • 26
2

I tried with http-server by installing it globally

npm install -g http-server

then moved to the dist/project-folder and tried with

http-server -o

output in console

[Fri Sep 13 2019 15:19:57 GMT+0530 (India Standard Time)] "GET /" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132
Safari/537.36"

Then this solution worked with all the steps same but instead of using http-server try angular-http-server

It worked for me.

UniCoder
  • 2,253
  • 21
  • 22
1

ng serve will work as normal, and it doesn't require a prior build. It generates files in memory, and has some additional features like auto reload.

Tatsuyuki Ishi
  • 3,500
  • 3
  • 25
  • 37
0

Install this globally (or locally)

npm i -g lite-server

Run this command (replace project-name)

lite-server --baseDir="dist/project-name"
tottomotto
  • 1,713
  • 1
  • 17
  • 27