22

I'm new to Angular 2 CLI. As per tutorial, there is command provided called ng create <project_name>. My scenario is creating project in existing directory.

It throws error like below:

Directory 'angular-basic' already exists.

norbitrial
  • 14,716
  • 7
  • 32
  • 59
Sachin
  • 283
  • 1
  • 3
  • 9

7 Answers7

31

Since the ng init command is removed from the CLI commands what you can do is specify the directory. You might have to do some overwrites and modifications by yourself for some of the existing files.

You can pull it off with

ng new [app_name] --directory=directory_path

Example assume that your terminal is opened in angular-basic directory and you have to add the cli to angular-basic project, ng new angular-basic --directory . would create the workspace in current directory which is angular-basic.

UPDATE:

Reason that it has been removed is because of the message of having two ways to create a project and because people were using it to upgrade (poorly) - From cli team @Brocco's tweet.

Amith
  • 730
  • 6
  • 22
Saiyaff Farouk
  • 5,103
  • 4
  • 24
  • 38
18

This is easy enough.

Run the following command from the existing directory which will serve as your application root.

$ ng new current-directory --directory=./ --skip-install

This uses three arguments as:

  1. current-directory whatever your project name will be or current location where terminal is open.

  2. --directory=./ this what does the magic, it instructs angular-cli to use existing folder as project root.

  3. --skip-install this one is totally optional, only prevents installation of all npm dependencies upfront.

Just in case you see an error like The Schematic workflow failed. See above., clear the contents of your directory, specially README.md, .gitignore etc. and let angular-cli create them for you.

pixlboy
  • 1,452
  • 13
  • 30
8

try this command:

ng new appName --directory ./
Temo Kiknadze
  • 269
  • 3
  • 8
  • While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 02 '20 at 16:29
4

Try ng init will create project inside your existing directory then do ng serve.

ng init <project-name> [options]
Creates a new Angular project in the current folder.

Options:
 --dry-run only output the files created and operations performed, do
 not actually create the project.

 Alias: 'd'.
 --verbose output more information.

 Alias: 'v'.
 --skip-npm do not run any npm command once the project is created.
 --name The name of the project to create.

For New Angular 2.x Project

ng new foo
cd foo
npm link angular-cli
ng serve

Refer: https://cli.angular.io/reference.pdf

Venkat.R
  • 7,420
  • 5
  • 42
  • 63
0

If your directory structure is: grandParent/parent/yourProject, go to the parent folder in CLI. and do ng new yourProject. It will create the project in the existing directory without creating additional one. (Tried with CLI Version 6.3 and 9.1)

Deb
  • 5,163
  • 7
  • 30
  • 45
0

Go in the directory in which you want to create Angular Project

Type this command :

ng new yourappname --directory ./

Aniruddha
  • 39
  • 1
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 20 '22 at 09:53
-3

you can use yarn package manager to resolve the dependencies of the existing project. For more information check this answer.

Naresh
  • 941
  • 9
  • 22