Sub topic 2
Creating a workspace with ng new
This chapter assumes you have Node.js and npm installed, can use a terminal, and know basic TypeScript. You will create a workspace, answer the prompts, and learn which options matter.
What ng new produces
A workspace is a folder that holds one or more Angular projects plus the configuration shared by all of them. The ng new command creates that folder, writes the starter files, runs npm install, and initializes a Git repository.
Install the CLI once, then create the workspace:
npm install -g @angular/cli
ng new my-app
cd my-app
ng serveThe name my-app becomes the folder name and the project name in angular.json. Angular project names must start with a letter and may contain letters, digits, and hyphens. If the name is invalid, the CLI stops with an error before it creates anything.
Answering the prompts
When you run ng new without flags, the CLI asks questions. The exact set depends on your CLI version, but these are typical.
| Prompt | What it controls |
|---|---|
| Stylesheet format | CSS, Sass, or Less for component styles and `src/styles.*` |
| Server-side rendering (SSR) and static site generation | Whether the CLI adds a server entry point and rendering setup |
| Zoneless change detection | Newer versions only. Whether to drop Zone.js. |
You can skip the prompts by passing flags. This is useful in scripts and tutorials:
ng new my-app --style=scss --routing --ssr=false --skip-gitUseful flags:
--stylesets the stylesheet format.--routingcreates the route configuration file.--ssrturns server-side rendering on or off.--skip-installskipsnpm install. Run it yourself later.--skip-gitskips Git initialization.--dry-runlists what would be created and writes nothing.
What the command does, step by step
Because step C uses schematics (code generators that the CLI runs), the files match the options you chose. For example, choosing --routing adds app.routes.ts, and choosing SCSS produces styles.scss.
Common errors
- Unsupported Node.js version. Each Angular release supports specific Node.js versions. If
ngprints a version warning, upgrade or switch Node.js, for example with a version manager. ngis not recognized. The global npm folder is not on yourPATH. Add it, or run the CLI withnpx @angular/cli new my-app.- Install fails halfway. Network or permission problems often cause this. The files already exist, so
cdinto the folder and runnpm installagain. - Creating a workspace inside another one. Run
ng newfrom a plain parent folder. Inside an existing workspace, the CLI would create a nested workspace.
Exercise
You want a new workspace named shop that uses SCSS, skips Git initialization, and does not install packages yet. Which command do you run, and how do you finish setup later?
Show answer
Run ng new shop --style=scss --skip-git --skip-install. Later, cd shop and run npm install, then ng serve.