Angular CLI Error: The serve command requires to b

2020-01-24 10:46发布

问题:

When running the terminal commands ng server or ng serve --live-reload=true, I'm getting this issue:

The serve command requires to be run in an Angular project, but a project definition could not be found.

回答1:

I was also getting this issue and solved by running below command.

ng update @angular/cli --migrate-only --from=<WhateverVersionYouAreCurrentlyOn>

e.g.

ng update @angular/cli --migrate-only --from=1.7.3

getting ref from here https://github.com/angular/angular-cli/issues/12215#issuecomment-433593036



回答2:

make sure that you are running the command in the application root folder..



回答3:

if you have downloaded a project,do in the project

npm install



回答4:

Finally, the command below fixed the issue for me!

ng update --all --force


回答5:

Angular Cli Error: The serve command requires to be run in an Angular project, but a project definition could not be found

Problem was missing angular.json files.

    ng update --all --force

Tested in Angular 7+



回答6:

The same problem I came across, but I solved by following these steps of code.

Step 1) npm install -g @angular/cli
Step 2) ng new my-angular-project
Step 3) cd my-angular-project
Step 4) ng serve --open


回答7:

Make sure you have set the new angular version configuration in your project. The new angular cli uses angular.json and not .angular-cli.json for its configuration.

Follow migration guide.



回答8:

In your Package Manager Console run the command

PM> cd NameofApp 

then you can try to run the command there.For instance

PM > ng generate component Home


回答9:

It was happening in my existing project as I tried to update to latest node and npm packages:

  1. Uninstall global: npm uninstall -g angular-cli
  2. Reset Cache: npm cache clean or npm cache verify (for npm version > 5)
  3. Install latest: npm install -g @angular/cli@latest


回答10:

This happens when you try to run the Angular command "ng s" or "ng serve" on the other folder instead of the root folder of the Angular application.

Also upgrade the Angular using the following command:

ng update @angular/cli --migrate-only --from=1.7.4

This will sort it out.



回答11:

I had the same issue with Angular7 because we need to go the root folder before run your application. Go to the root folder of your app and run the command. It works perfectly for me.



回答12:

It was silly me but need to leave comment maybe I save someone else some hassle. Same message happens if you multitask and have multiple projects so some of project is nested inside project folder like projectFolder/subProject/projectOne so I was in projectFolder/subProject trying to run command ng serve.

Make sure you are in correct folder because same error will occur if you are in wrong folder and conf and build is missing!



回答13:

I came across the same error. The reason is that new angular cli update makes angular-cli.json redundant, and it is replaced with angular.json instead. My previous Angular Cli version is 1.7.4, so to make the change, I ran the following command, it will make the conversion for you:

ng update @angular/cli --migrate-only --from=1.7.4


回答14:

I faced the same issue when,

I had created new angular project using old angular-cli version (1.4.7) I then updated angular-cli using below commands (DO NOT DO THE BELOW TO UPDATE CLI)

  • npm uninstall -g @angular/cli
  • npm cache clean --force
  • npm install -g @angular/cli@latest

Now when I tried ng serve, i was getting same error like you

Angular Cli Error: The serve command requires to be run in an Angular project, but a project definition could not be found

DO BELOW TO UPDATE CLI

ng update @angular/cli --migrate-only --from=1.4.7


回答15:

As Usman said, i had a lot of trouble first time, I was following a tutorial. I had not set my directory to the project directory hence got this error.

I solved the issue by

  1. Setting the correct root directory to where my project was cd myproject
  2. Compile the app - ng serve

That was it.



回答16:

This error occurs when the project you are running is not an angular project. Though you have downloaded an angular project but have not installed all the dependencies thats why the ng serve command is not available to you.

Just navigate to the path where the project is stored and use the command

npm install

(Note - Node.js should be installed in your system and if you are using Angular 2 or above angular cli should also be installed in your system before you run this command. To check if the node.js is installed n your system 1) Open cmd (any path- as node should be globally installed in your system) 2) use command

node -v
npm -v

to get node and npm version)

One more important thing: The angular cli version won't make a difference if the version installed in your system is higher than the version required by the project. It will give a warning but you can ignore the warning.



回答17:

This error may be occur when we are in wrong directory.so use cd command to change your directory. It's not very evident from the above comment that which specific command is giving this error so this answer is based on assumption that you are running ng serve outside the root folder/actual project folder. That's why it's giving error because cli commands require conf & build files to run the cli build.

These should be the steps:
npm install -g @angular/cli  //corrected from 'angular-cli'
ng new projectname
cd projectname
ng serve
open http://localhost:4200


回答18:

Also make sure if you are running ng serve that you stop it first...



回答19:

Solved. I was getting same issue on ubuntu 18.04.01 LTS. No above options worked as I was using NVM (Node Version manager.) Solution is
before creating project,use command
nvm use 10
(my node version was 10.15.0, so I used "nvm use 10")
It will show you output-
Now using node v10.15.0 (npm v6.4.1)
Versions will vary as per individual systems. then create new project.



回答20:

You can update the angular with command 'ng update --all'. It will take time but make sure your all components are up to date.



回答21:

Please check whether you are inside your project folder or not and try the command ng serve again

I got the same error since I tried ng serve command outside of my project

Example :: let's say you have a project with name "Ecommerce" traverse into the folder and try the command ng serve open terminal cd Ecommerce ng serve



回答22:

In my case, I forgot to change my directory, so just after running the command :

ng new AngularProject

execute another command :

cd AngularProject

and ng serve worked for me.



回答23:

This fixed my problem:

ng generate component nameComponent --module app.module


回答24:

This happens because we are hitting ng serve command in some other path.This can be solved by hitting ng serve or npm start command on the path where exactly the our project resides(take path until the folder which contains src,node_modules,etc.)

F:\project\AngularDemo\AngularDemoapp> ng serve



回答25:

Tried all the above But for me it was solved by

npm start



回答26:

Make sure you have created angular App using below command, then running ng serve in created Project folder.

ng new Project_Name



回答27:

Happened to me when didn't install angular core:

Angular CLI: 9.0.0-rc.0
Node: 11.12.0
OS: darwin x64
Angular: undefined
...

So I run

npm i @angular/core

which gave me ng -v

Angular CLI: 9.0.0-rc.0
Node: 11.12.0
OS: darwin x64
Angular: 8.2.13
... core

Then I run

ng update @angular/cli --migrate-only --from=8.2.13

removed node_modules

npm install
cd <project name>
ng serve

And it worked!

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ ** ℹ 「wdm」: Compiled successfully.



回答28:

I would suggest to confirm if you are in the directory of the project.



回答29:

I am using ionciv1, in the file ionic.config.json delete the angular type key and it worked correctly

example:

  {
  "name": "nombre",
  "integrations": {
    "cordova": {}
  },
  "type": "angular", // delete
  "gulpStartupTasks": [
    "sass",
    "templatecache",
    "ng_annotate",
    "useref",
    "watch"
  ],
  "watchPatterns": [
    "www/**/*",
    "!www/lib/**/*"
  ],
  "browsers": [
    {
      "platform": "android",
      "browser": "crosswalk",
      "version": "12.41.296.5"
    }
  ],
  "id": "0.1"
}

solved

     {
      "name": "nombre",
      "integrations": {
        "cordova": {}
      },
      "gulpStartupTasks": [
        "sass",
        "templatecache",
        "ng_annotate",
        "useref",
        "watch"
      ],
      "watchPatterns": [
        "www/**/*",
        "!www/lib/**/*"
      ],
      "browsers": [
        {
          "platform": "android",
          "browser": "crosswalk",
          "version": "12.41.296.5"
        }
      ],
      "id": "0.1"
    }


回答30:

to reinstall back freshly the application is way better rather than update or editing some config that is not recognize by npm due to missing config or we are not really sure what is wrong.

I choose @Tinu solution is work

Step 1) npm install -g @angular/cli

Step 2) ng new my-angular-project

Step 3) cd my-angular-project

Step 4) ng serve --open

just made backup for our current source code and put it back to new angular project that be created.