Onur Ata Asar
Onur Ata Asar

Reputation: 639

Error: This command is not available when running the Angular CLI outside a workspace

I'm fairly new to Angular. But when I try to start my project with

ng serve --open

I got the following error:

error

My project structure is like this:

structure

What can be the problem?

Upvotes: 62

Views: 466301

Answers (15)

Georgii Vlasov
Georgii Vlasov

Reputation: 422

In my case, the problem was the following. A few days before exploring Angular, I just tried JHipster, with Angular.

My bad; I used the JHipster command in the root folder on a Mac (users/${currentUser}). So, I have the angular.json file in there. It seems that Angular scans folders from the root, stumble upon this file and breaks its internal logic.

After cleaning up this folder from JHipster files (including angular.json) everything works smoothly. I suggest you to check your root folder for the presence of redundant files that might break Angular.

Upvotes: 1

Sarath Kumar pgm
Sarath Kumar pgm

Reputation: 131

i got this error because of im running the app from outside of the folder so I did a cd app-name then applied npm install after that ng serve -o

Upvotes: 0

nitinsingh9x
nitinsingh9x

Reputation: 696

Navigate to root folder in terminal and then run angular cli command. Below command will put you in root directory from current location:

cd angular_t-project

Upvotes: 10

karl li
karl li

Reputation: 1486

In my case, that's the missing file issue: for some unknown reason, my Angular project lost its angular.json file, which should be located in the project root directory.

As this was a tutorial project (angular-tour-of-heroes), I downloaded a sample from the official website. By comparing the two projects, soon I found the difference. I copied it from the sample to mine and did a minor change in angular.json (different project name).

Finally I ran npm install and ng build, and the world returned...

So for a similar issue, maybe you should check the angular.json file first because this JSON content defines "sourceRoot".

Upvotes: 12

Sergey Samoylov
Sergey Samoylov

Reputation: 9

Just go to the folder of your newly created project.

For example, if you created hello-world:

ng new hello-world
cd hello-world
ng serve

The reason for your mistakes (and mine, btw) was that we were trying to start the server from outside the project folder.

Upvotes: 0

GRCarvalho
GRCarvalho

Reputation: 134

Well, it seems that there are more than one answer for this problem and none of the given here solved my problem.

I am running inside the project's folder, and I was working with it normally. Then I needed to create a new component and I stumbled on this error.

All the other commands worked, like ng serve, etc.. I also checked the angular.json file, and it was there, not corrupted...

I made a full new project from zero, and went inside the projects folder, tried ng n c myComponent and I got that error again...

This started after an ng cli update.

Upvotes: -1

Bhavesh
Bhavesh

Reputation: 122

I was facing the same issue. It turns out it was related to the position in my directory.

If you want to build or serve an Angular project, you have to be in the root directory of your project.

Hint: To determine if you're in root project, type the "ls" or "dir" command. If the command prompt shows you all Angular files, like "angular.json", "package.json", etc. you're in the root folder.

Once you're in the root folder, you could execute these commands.

Upvotes: 0

Nishu Singh
Nishu Singh

Reputation: 51

It's easy:

Note: Please stay in your root folder and then run the commands in the terminal. (Whatever name of your app you gave while creating the app using the ng new appname command, use that "appname" only in the following command.)

Step 1: Run command cd appname

Step2: Then run ng serve -o

It must solve the issue if other ways are not working.

Upvotes: 5

Jeeva M
Jeeva M

Reputation: 21

Error: This command is not available when running the Angular CLI outside a workspace.

Check you are in the file or not (folder → cd project file).

Upvotes: 1

yudhishtir gaddipati
yudhishtir gaddipati

Reputation: 81

When it becomes difficult on ng new my-project, use the following (--save-dev) to update the local version to the latest in your folder where you wish to create a new project:

npm install --save-dev @angular/cli@latest

Upvotes: 7

noxx
noxx

Reputation: 84

As everyone else has said, you have to be in the root folder.

If you using Visual Studio 2019/2022, use the Terminal/PowerShell:

cd C:\Users\<user>\source\repos\<solution>\<projectname>\ClientApp

Upvotes: 0

Eswar_Avala
Eswar_Avala

Reputation: 509

To fix error “This command is not available when running the Angular CLI outside a workspace Error”, do right-click on your project's name in Visual Studio Code and click the “Open in Integrated Terminal” option.

It would open the project to your terminal and the error would be fixed. Now you would be able to run the command “ng serve –open” or "ng serve" into your terminal without any issue.

Upvotes: 50

Ravindranath K.
Ravindranath K.

Reputation: 41

This issue arises when you try to run ng serve in another folder; i.e., instead of the root folder where your README.md file and other .json configurations are present.

Upvotes: 1

Richard Jessop
Richard Jessop

Reputation: 972

This may happen if you unzip a project file, and the project is actually further down in the unzipped folder hierarchy. Like others have said, check your current working directory and verify that you are in the project root folder.

Upvotes: 0

Linh Dao
Linh Dao

Reputation: 1673

In my case, angular.json file is automatically deleted when setting up Nx Console. I just use git to revert the angular.json deletion to fix this error.

Upvotes: 39

Related Questions