ng new in Angular CLI

We use the ng new to create a new Angular project. It is part of the Angular CLI and It creates a workspace and an initial application with the predetermined configurations. This helps us to get started with a new Angular application quickly.

ng new

Without the new command, it will take a lot of effort to create simple applications like Hello World. To manually create an angular application, you need to follow these steps.

  1. Create a new root directory for the project.
  2. Create application folders like src, app, assets, etc under the project root.
  3. Next, we need to create configuration files such as Angular.json, tsconfig.json, etc.
  4. Create the package.json file and install the necessary dependencies using npm.
  5. Create the  app.component.ts, app.component.html, app.module, main.ts , index.html, etc.

The ng new does all of the above under the hood.

ng new syntax

The syntax of ng new is as follows.

name: The name of the new workspace and initial project.

options: The options are optional parameters. The list of options is specified at the end of this article.

ng new examples

The following examples show how to use ng new in Angular.

Creating a new Angular application

To create a new workspace and new project, run the following command.

The above code will create a new folder HelloWorld in the current working folder. It will also create the hello-world application inside the src folder.

Note that Angular uses the dash-case (or kebab-case) for project names, file names, component selectors, etc. Hence HelloWorld will change to hello-world.

You can run the application using the command ng serve.

Creating the new Angular project using the ng new command

Creating only workspace

To create only a workspace without creating the angular application, use the --create-application=false option

The above command creates a folder WorkspaceOnly under the current folder. It will create the node_modules folder and installs all the dependencies. But it will not create any application or create a src folder.

Angular ng new workspace only

You can add the project later by using the command ng generate application

The above command creates a new application with the name hello-world inside the folder projects/hello-world. Since it is the first project, it will be marked as the default project. You can also create more than one project inside the same workspace, and all of them will share the same dependencies. Refer to the tutorial create Multiple Projects in a workspace for more information.

Custom workspace name

ng new uses the same name for the workspace folder and application name. We can use the projects option to choose a different folder name for the workspace.

The code below creates the folder angular-projects under the current directory and the application hello-world inside the src folder.

The code below creates the workspace angular-projects under the current folder. But it will not create any application. Note that if we do not specify the hello-world in the command, the command will ask for the name, but it will be ignored.

Inline template and styles

The ng new command generates the separate template (app.component.html) and CSS file (app.component.css). You can use the --inline-style=true --inline-template=true option to inline both template and style in the app.component.ts.

It will generate the following app.component.ts

Standalone API

Starting from Angular 17, ng new will make use of the Standalone API. i .e., by default it will create the standalone components. You will not see the app.module in the generated project.

You can disable it using the flag --standalone=false. It will fall back to module based component.

But in Angular 15 & 16, Module based components are default. To create the standalone components use the --standalone=true flag.

ng new options

The list of all available options of ng new project

OptionsAliasDESCRIPTIONValue TypeDefault Value
--collection-cA collection of schematics to use in generating the initial application.String
--commitInitial git repository commit information.booleantrue
--create-applicationCreate a new initial application project in the 'src' folder of the new workspace. When false, creates an empty workspace with no initial application. You can then use the generate application command so that all applications are created in the projects folderbooleantrue
--defaultsDisable interactive input prompts for options with a default.booleanfalse
--directoryThe directory name to create the workspace instring
--dry-run-dRun through and reports activity without making any changes.booleanfalse
--force-fForces overwriting of any existing files in the project folderbooleanfalse
--helpShows a help message for this command in the consoleboolean
--inline-style-sUse inline style rather than the external StyleSheet file. does not create external Stylesheetsbooleanfalse
--inline-template-tDoes not create an external template file for the component. Specifies if the template will be in the ts file.booleanfalse
--interactiveEnable interactive input promptsbooleantrue
--minimalInstalls a minimal set of features. does not create test files.booleanfalse
--new-project-rootThe path where new projects will be created, relative to the new workspace root.stringprojects
--package-managerThe package manager used to install dependencies.npm | yarn | pnpm | cnpm
--prefix-pThe prefix to apply to generated selectors for the initial project.
--routingGenerates a routing module. If the option is not specified, it will ask for the confirmation
--skip-git-gDo not initialize a git repository.booleanfalse
--skip-installDo not install dependency packages.booleanfalse
--skip-tests-SDo not generate "spec.ts" test files for the new project.
--ssrCreates an application with Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering) enabledbooleanfalse
--standaloneCreates an application based upon the standalone API, without NgModulesbooleantrue
--strictCreates a workspace with stricter type checking and stricter bundle budgets settings. This setting helps improve maintainability and catch bugs ahead of timebooleantrue
--styleThe file extension or preprocessor to use for style files.css | scss | sass | less
--view-encapsulationSpecifies the view encapsulation strategy.Emulated | None | ShadowDomEmulated

Summary

  • ng new is Angular CLI command
  • We use it to create a new Angular workspace along with an initial project.
  • You can use the option --create-application=false to create only the workspace without using the initial project. Make use of the ng g application command to add project to the workspace.
  • Use the --standalone=true to create application based on Standalone API
  • The --inline-style=true --inline-template=true will make the style & template inline. No external files are generated.

Reference

Command Reference

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top