Video lecture
Watch on YouTube(opens in a new tab)Sub topic 4
Creating a New App with ng new
This chapter assumes the Angular CLI is already installed globally, so ng version prints a version number. It also assumes you have a supported Node.js LTS release and npm, and that you are comfortable in a terminal. Here you will use ng new to generate a workspace, answer its prompts, and fix the errors people hit most often.
What ng new does
ng new creates a workspace: a folder that holds one or more Angular projects plus the shared configuration they use. The first project in the workspace is the application, and it has the same name as the workspace. A single command carries out the whole setup.
Running the command
Run it from a folder that is not already inside an Angular workspace:
ng new my-app
cd my-app
ng serve --openThe CLI then asks a few questions. The exact list depends on your CLI version, but these are the usual ones:
| Prompt | Typical choice | Effect |
|---|---|---|
| Stylesheet format | CSS or SCSS | Sets the file extension for component styles and the global `styles` file |
| Server-side rendering (SSR) and static site generation (SSG) | No, while learning | Adds server entry files and `@angular/ssr` |
| AI tooling configuration (newer versions) | None | Writes config files for AI coding assistants |
Once you answer, the CLI writes the files and runs npm install. Installing usually takes the longest.
Skipping prompts with flags
In scripts and CI pipelines you can pass every answer as a flag, so the command never stops to ask a question:
ng new my-app --style=scss --ssr=false --prefix=shop --skip-git| Flag | Purpose |
|---|---|
| `--style` | `css`, `scss`, `sass`, or `less` |
| `--ssr` | Enable or disable server-side rendering |
| `--prefix` | Selector prefix for generated components (default `app`, giving `app-counter`) |
| `--skip-install` | Generate files without running `npm install` |
| `--skip-git` | Skip creating a Git repository |
| `--skip-tests` | Skip generating `.spec.ts` test files |
| `--dry-run` | Show which files would be created and write nothing |
| `--defaults` | Accept the default answer for every prompt |
Common errors
- Unsupported Node.js version. The CLI stops and names the Node versions it supports. Install a matching LTS release with a version manager such as
nvm, then run the command again. - Running inside an existing workspace.
ng newrefuses to run inside another workspace. Move to a parent folder first. To add a second app to an existing workspace, useng generate applicationinstead. - Invalid project name. The name must start with a letter. Use lowercase words joined by dashes, such as
my-app. Spaces and names that start with a digit are rejected. - npm install fails. The files are already generated at this point, so you don't need to start over. Fix the network or registry problem, then run
npm installinside the new folder. ng: command not found. The global npmbinfolder is not on yourPATH. As a workaround, runnpx @angular/cli new my-app.
Exercise
You need a CI job that creates an Angular app named "reports" with SCSS styles, no server-side rendering, the component prefix "rpt", and no Git repository, without stopping for any prompt. What command do you run?
Show answer
ng new reports --style=scss --ssr=false --prefix=rpt --skip-git (add --defaults to accept the default answer for any remaining prompts in your CLI version).
Exercise
ng new finished generating files but failed during npm install because of a network timeout. What is the fastest way to recover?
Show answer
Change into the generated folder (cd my-app) and run npm install again once the network works. You don't need to delete the folder or rerun ng new, because the files already exist.