61

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.

5
  • 1
    I am new to angular4, may I know what is dist, build Commented Apr 13, 2017 at 10:18
  • 2
    @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... Commented Sep 14, 2017 at 9:02
  • Below stack url can be helpful stackoverflow.com/a/53300931/7165958 Commented Nov 14, 2018 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? Commented Feb 10, 2019 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) Commented Feb 11, 2019 at 10:37

11 Answers 11

80

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

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

1 Comment

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.
36

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.

5 Comments

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 I'm glad it helped!
How would I specify the IP address, e.g. http://0.0.0.0; port can be done via the -p flag.
Note: I wondered what the angular-http-server vs http-server rule was. npmjs.com/package/angular-http-server says that angular-http-server should not be used as a production server; only for development; whilst npmjs.com/package/http-server claims http-server can be used for production. nginx can also be used (see medium.com/bb-tutorials-and-thoughts/…)
@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.
19

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

Comments

6

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 

Comments

5

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.

Comments

4

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

1 Comment

The --prod option will compile the files and run it from streaming and not from folder /dist
3

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.

Comments

3

You don't need to install anything, Just use the following command.

npx lite-server --baseDir="dist/"

1 Comment

Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.
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.

5 Comments

Hi, I have ng serve working but its serving the dev files and not that dist folder. I want to test the dist folder using serve. whats the command for that?
angular-cli can't do that. You want an instant or production grade static server instead.
do you know any I could install?
The question is clear, and this answer doesn't answer it. Should have been a comment.
1

Install this globally (or locally)

npm i -g lite-server

Run this command (replace project-name)

lite-server --baseDir="dist/project-name"

Comments

0

I would recommend web dev server. I found it to be light and easy to use. It also has a nice watch option

From inside the dist/ folder I run this command

% wds -p 8080 --watch   
Web Dev Server started...

Comments

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.