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

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

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

The error "This command is not available when running the Angular CLI outside a workspace" occurs when your terminal isn't positioned in the root directory of an Angular project when running the ng serve command.

To solve the error, navigate to the root directory of your project (where your angular.json file is) and rerun the command.

error this command is not available when running the angular cli

The most common cause of the error is issuing the ng serve --open command while your terminal isn't located in the root directory of your Angular project.

# Create an Angular project if you don't already have one

In case, you don't have an Angular project, you can create one with the following commands.

shell
# ๐Ÿ‘‡๏ธ install the `angular` CLI globally npm install -g @angular/cli@latest # ๐Ÿ‘‡๏ธ create a workspace ng new my-project # ๐Ÿ‘‡๏ธ run the application cd my-project ng serve --open

You can use the cd command to change your terminal to the root directory of your Angular project.

shell
# ๐Ÿ‘‡๏ธ list files and directories dir # ๐Ÿ‘‡๏ธ make sure to replace with the name of your project cd your-angular-project ng serve --open

change to angular root directory windows

And here is an example of how to do it on macOS and Linux.

shell
# ๐Ÿ‘‡๏ธ list files and directories ls # ๐Ÿ‘‡๏ธ make sure to replace with the name of your project cd your-angular-project ng serve --open

change to angular root directory

# Open your terminal in the root directory of your Angular project

Alternatively, you can open your terminal directly in the root directory of your Angular project (where your package.json) file is located.

Here is what the root directory of your Angular project should look like. This is the directory where you should open your shell.

root directory of angular project

For example, on Windows:

  1. Open the root directory of your Angular project (where angular.json and package.json are located) in explorer.

  2. Press Shift and right-click in Explorer.

open powershell in angular root directory

  1. Click on "Open PowerShell window here".

  2. Run the ng serve --open command.

PowerShell
ng serve --open

On macOS and Linux:

  1. Open the root directory of your Angular project (where angular.json and package.json are located).
  2. Right-click and click "Open in terminal".

click open terminal macos linux

  1. If you don't see the "Open in terminal" option, right-click and press E on your keyboard.

  2. Run the ng serve --open command.

PowerShell
ng serve --open

If you don't have an Angular project, you can create one with the following commands.

shell
# ๐Ÿ‘‡๏ธ install the `angular` CLI globally npm install -g @angular/cli@latest # ๐Ÿ‘‡๏ธ create a workspace ng new my-project # ๐Ÿ‘‡๏ธ run the application cd my-project ng serve --open

# Open your terminal in your project's root directory within your IDE

An alternative way to solve the error is to open your terminal in your project's root directory in your IDE (e.g. Visual Studio Code).

This is what the root directory of my Angular project looks like in Visual Studio Code.

root directory of angular project vscode

You can press CTRL + ` (Backtick) on your keyboard to open the Visual Studio code terminal.

You can also open the terminal in Visual Studio code by pressing CTRL+Shift+P and then type "View: Toggle Terminal".

open vscode terminal

Once you open the terminal in your project's root directory, run the ng serve --open command.

shell
ng serve --open

run ng serve open command

# Using the "Open in integrated terminal" option

An alternative approach to solve the error is to:

  1. Open your Angular project in VSCode.
  2. Right-click on the root directory of your project (the directory that contains your package.json file).

vscode open in integrated terminal 3. Click on "Open in Integrated Terminal" as shown in the gif above.

  1. Issue the ng serve --open command.
shell
ng serve --open

Make sure to right-click on the folder that contains your Angular project (the one that contains the package.json file).

I opened the root directory of my Angular project in VSCode, so I clicked directly in the left sidebar.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev