Sub topic 2
Creating a workspace with ng new
This chapter covers ng new, the command that creates an Angular workspace. It assumes you know basic TypeScript, HTML and CSS, and that Node.js and npm are installed. It also assumes you are comfortable in a terminal. It looks at what the command asks you, what it produces, and which errors tend to appear.
What ng new does
ng new is a CLI command that runs the application schematic (a code-generating recipe) inside a new directory. It does four jobs in order:
- Creates a folder named after your project.
- Writes the configuration, source and test files.
- Runs your package manager to install dependencies.
- Initializes a Git repository and makes a first commit.
The result is a workspace. A workspace is a folder that holds one or more Angular projects plus the shared configuration for them. By default ng new creates a workspace with a single application whose files sit at the workspace root.
npm install -g @angular/cli
ng new my-app
cd my-app
ng serveThe first line installs the CLI globally. The second creates the workspace named my-app and asks some questions. The last two enter the folder and start the development server, which serves the app on http://localhost:4200 by default.
The interactive prompts
When you do not pass options, the CLI asks questions. The exact list depends on the CLI version, so read the prompts on your machine. Common ones are:
| Prompt | What it controls |
|---|---|
| Stylesheet format | CSS, Sass (SCSS), or Less for component styles |
| Server-side rendering (SSR) and static site generation | Whether to add server rendering setup |
| AI tooling or similar extras | Optional configuration files, in newer versions |
The answers are written into the generated files. For example, choosing SCSS changes the styles entry in angular.json to src/styles.scss and changes the default style extension for generated components.
You can skip the prompts by passing flags. This is useful in scripts and continuous integration (CI), where nobody is there to answer.
ng new my-app --style=scss --routing --ssr=false --skip-git --package-manager=npmEach flag answers one question:
--style=scssselects the stylesheet format.--routingadds the route configuration file. Newer versions create it by default, so checkng new --helpfor your version.--ssr=falseturns off server-side rendering.--skip-gitstops the Git initialization.--package-manager=npmpicks the tool used for installation. Other values includeyarn,pnpmandbun.
Useful options beyond the prompts
Several options are worth knowing, even if you rarely change them.
--dry-runprints the files the command would create, without writing anything. Use it to preview a new option.--skip-installcreates the files but does not install packages. This is handy when you want to choose the install step yourself, or when you are offline.--directorycreates the workspace in a folder with a different name than the project.--prefixsets the selector prefix for generated components. The default isapp, so a component generated asproduct-listgets the selectorapp-product-list.--inline-styleand--inline-templateput styles and templates inside the component class instead of separate files.--minimalcreates a workspace without test setup. It is meant for experiments, not for real projects.--create-application=falsecreates an empty workspace with no application. You then add projects later. This is the starting point for multi-project workspaces, covered in a later chapter.
ng new my-app --dry-runThe dry run lists lines such as CREATE my-app/angular.json, so you can see the full file set before committing to it.
What you get
The command creates a layout like this. Names vary slightly between CLI versions.
my-app/
├── .vscode/
├── public/
├── src/
│ ├── app/
│ ├── index.html
│ ├── main.ts
│ └── styles.css
├── .editorconfig
├── .gitignore
├── README.md
├── angular.json
├── package.json
├── tsconfig.json
├── tsconfig.app.json
└── tsconfig.spec.jsonLater chapters examine each of these in turn. For now, note the split between two groups:
- Root files (
angular.json,package.json,tsconfig*.json) configure tooling, builds and dependencies. - The
srcfolder holds the code that runs in the browser.
Older versions use an assets folder inside src instead of the top-level public folder. If your tutorial or team uses an older CLI, expect that difference.
Version matters
The CLI you install decides which Angular version the workspace uses. ng new writes dependencies in package.json at the same major version as the CLI. A global CLI at one major version creates a project on that same version.
Check your version before you start:
ng versionTo create a project with a specific version without changing your global install, run the CLI through npx:
npx @angular/cli@18 new legacy-appThis downloads that CLI version for one run. It is a safe way to match an existing team project.
Inside a project folder, ng uses the local copy of the CLI from node_modules when one is present. The global install is mainly used to run ng new and to start work outside a workspace. A mismatch between global and local versions is a common source of confusing output, so look at the Angular CLI line in ng version.
Naming rules
The project name ends up in angular.json, package.json and the folder name. The CLI validates it. Names must:
- start with a letter,
- contain only letters, numbers and dashes (and some versions allow dots and underscores),
- not clash with reserved words such as
testin some contexts.
A name like my-app is safe. A name like 2fast or my app fails.
Common errors
ng: command not found. The global install is missing, or npm's global bin folder is not on yourPATH. Install again, or usenpx @angular/cli new my-app.- Permission errors on global install (
EACCES). On Linux and macOS, avoidsudo npm install -g. Configure npm to use a user-owned prefix, or use a Node version manager. - Unsupported Node.js version. Each Angular major version supports certain Node.js versions. The CLI prints a message and stops when yours is outside the range. Upgrade Node.js or use an older CLI.
- The folder already exists.
ng newrefuses to write into a non-empty folder. Choose another name or remove the folder. - Install fails halfway. The files are already written. Run
npm installinside the folder once the network or registry problem is fixed. This is the case--skip-installcreates on purpose. - Running
ng newinside another workspace. The new project is nested in the existing one, which later confuses the CLI about whichangular.jsonto use. Run it in a neutral folder.
Verifying the result
After creation, confirm the workspace works:
cd my-app
ng build
ng test --watch=falseng build should write output to dist/my-app without errors. ng test --watch=false runs the starter tests once and exits. If both pass, the workspace is healthy and you can begin changing it.
Exercise
You need a new workspace in a CI script. It must use SCSS, must not set up server-side rendering, must not create a Git repository, and must not install packages yet. Write the command, and name one way to preview its output first.
Show answer
ng new my-app --style=scss --ssr=false --skip-git --skip-install. To preview, add --dry-run to the same command. It lists the files that would be created and writes nothing.
Exercise
A teammate runs ng new inside the my-app folder of an existing workspace and later finds that ng build picks the wrong angular.json. What went wrong, and what command should they have used to add a second application?
Show answer
The new project was nested inside the existing workspace, so the CLI found two angular.json files. They should have used ng generate application in the existing workspace root, which adds a project to the same angular.json.