Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Angular Dictionary
  3. Angular CLI

Angular CLI

Since: Angular 2(2016)

The Angular CLI (Command Line Interface) is a command-line tool for creating, building, testing, and deploying Angular applications. By running ng commands in a terminal, you can streamline the entire development workflow — from project scaffolding and component generation to production builds — without manual configuration. It is installed via npm and used globally or locally within a project.

Main Commands

CommandOverview
ng new <project-name>Creates a new Angular project. Generates the folder structure, configuration files, and initial source code.
ng serveStarts a local development server with live reloading. Accessible at http://localhost:4200/ by default.
ng buildCompiles and bundles the application. Add --configuration production for an optimized production build.
ng generateGenerates boilerplate files (components, services, pipes, etc.). Abbreviated as ng g.
ng testRuns unit tests using Karma (or Jest, depending on configuration).
ng lintRuns static analysis on the code. Requires ESLint or a similar linter to be configured.
ng add <package>Adds a library to the project. Automatically updates configuration and installs dependencies.
ng updateUpdates Angular and related packages to newer versions.
ng versionDisplays the versions of Angular CLI and related packages.

Sample Code

Creating a new project and starting the development server.

# Install Angular CLI globally
npm install -g @angular/cli

# Create a new project named my-app
ng new my-app

# Move into the project directory
cd my-app

# Start the development server
ng serve --open

Generating a component, service, and pipe within an existing project.

# Generate a component (creates app/user-card/ directory)
ng generate component user-card

# Abbreviated form
ng g c user-card

# Generate a service
ng g service user

# Generate a pipe
ng g pipe truncate

# Generate a guard
ng g guard auth

Running a production build and checking the output.

# Production build (outputs to dist/ directory)
ng build --configuration production

# Check the output files
ls dist/my-app/

ng generate Subcommands

SubcommandAliasGenerated Files
component <name>cComponent class, template, style, and spec files.
service <name>sService class and spec file.
pipe <name>pPipe class and spec file.
directive <name>dDirective class and spec file.
guard <name>gGuard class and spec file.
module <name>mNgModule class.
interface <name>iTypeScript interface file.
enum <name>eTypeScript enum file.
class <name>clTypeScript class file.

Customizing angular.json

The angular.json file is the central configuration file for an Angular project. It controls build settings, file replacement (such as per-environment configuration), and asset management.

{
  "projects": {
    "my-app": {
      "architect": {
        "build": {
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all"
            }
          }
        }
      }
    }
  }
}

Summary

The Angular CLI is an essential tool for Angular development. With ng new you can scaffold a project in seconds, ng serve starts a development server with live reload, and ng build produces an optimized production bundle. The ng generate command reduces boilerplate by creating components, services, pipes, and more in a consistent format.

All CLI settings are centrally managed in angular.json, and per-environment configuration is handled via the fileReplacements mechanism. Once you are comfortable with the basic commands, explore ng add (for library integration) and ng update (for version upgrades) to keep the project up to date.

Common Mistake: Running ng commands outside the project directory

Commands like ng serve and ng generate must be run from inside the Angular project directory (where angular.json exists). Running them from a parent directory causes an error.

# NG: running from outside the project directory
cd ~
ng serve
# Error: Could not find an Angular project
# OK: move into the project directory first
cd my-app
ng serve

Common Mistake: Forgetting --open with ng serve

Running ng serve without --open starts the server but does not open the browser automatically. Add --open (or -o) to open http://localhost:4200/ automatically.

# Without --open: server starts but browser does not open
ng serve

# With --open: browser opens automatically
ng serve --open

Common Mistake: Committing the dist/ directory to git

The dist/ directory contains build artifacts and should not be committed to version control. It is listed in .gitignore by default. Always regenerate it on the server using ng build --configuration production.

# NG: manually adding dist/ to git
git add dist/

# OK: dist/ is already in .gitignore — leave it there
# Regenerate on the server with:
ng build --configuration production

If you find any errors or copyright issues, please .