Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-cli "ng build" doesn't produce a working project?

Tags:

(I'm new on planet Angular2 (emigrated from dying planet
Flex/Actionscript, so please forgive this naive question)

Have I made a fatal error in thinking that after running the "ng build" command on my project from Angular CLi, I would end up with a functioning project in the directory "dist" – which I could just run in a browser, put up on a server?

I end up with a folder full of correctly named stuff, etc. Is there a step I am missing here?

enter image description here

like image 949
spring Avatar asked Jun 01 '16 02:06

spring


People also ask

Does ng build default to production?

Production is the new default When you create a new Angular project, the commands ng build and ng serve use the production build by default.

What is the output of NG build?

Compiles an Angular app into an output directory named dist/ at the given output path. Must be executed from within a workspace directory. Uses the webpack build tool, with default configuration options specified in the workspace configuration file (angular.

How does ng build prod work?

The ng build command is intentionally for building the apps and deploying the build artifacts. The command does not generate an output folder. The output folder is – dist/. The ng serve builds artifacts from memory instead for a faster development experience.


2 Answers

I'm new to angular, and just ran into the same issue. You can open up and run the Index.html file in the browser directly from the file system, but you have to edit the path of the base href attribute in Index.html from:

<base href="/"> 

to:

<base href="./"> 
like image 180
user6927497 Avatar answered Sep 17 '22 19:09

user6927497


I thought I would cross post my answer from a similar question. Original Post.
I don't think the OP is a duplicate, so here;


You can achieve the desired outcome with the following cmd angular-cli command:

ng build --base-href /myUrl/

ng build --bh /myUrl/ or ng build --prod --bh /myUrl/

This changes the <base href="/"> to <base href="/myUrl/"> in the built version only which was perfect for our change in environment between development and production. The best part was no code base requires changing using this method.


To summarise, leave your index.html base href as: <base href="/"> then run ng build --bh ./ in angular-cli to make it a relative path, or replace the ./ with whatever you require.

This is the official angular-cli documentation referring to usage.

like image 29
Zze Avatar answered Sep 19 '22 19:09

Zze