10

I'm trying to run the command ng add @angular/pwa --my project name so that I can implement service workers in my angular project but the error that I'm returned is "The add command requires to be run in an Angular project, but a project definition could not be found." If anyone can shed some light on what I can do to get past this error it would be much appreciated. I was upgrading from Angular CLI version 1.49. I have a feeling it has something to do with the angularcli.json file since the new version uses angular.json file.

This is also an exception when I try to use ng serve : Workspace needs to be loaded before it is used. Error: Workspace needs to be loaded before it is used

this is my version details:

    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 7.0.1
Node: 10.12.0
OS: darwin x64
Angular: 5.2.11
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.10.1
@angular-devkit/core         0.3.2
@angular-devkit/schematics   7.0.1
@angular/cli                 7.0.1
@ngtools/webpack             1.10.2
@schematics/angular          7.0.1
@schematics/update           0.10.1
rxjs                         5.5.12
typescript                   3.1.1
webpack                      3.11.0
3
  • 1
    You have to add your project name after --project. Commented Oct 19, 2018 at 3:39
  • I have tried that, I am using my project name. Commented Oct 19, 2018 at 5:49
  • @angular/pwa was announced with the release of Angular 6. I suspect you'll have to update Angular to 6+ first. Commented Oct 23, 2018 at 2:29

7 Answers 7

13

You are right regarding the new version using the updated angular.json file. See the following links on updating the cli, they should help: Angular Cli Error: The serve command requires to be run in an Angular project, but a project definition could not be found and https://github.com/angular/angular-cli/issues/12215#issuecomment-433593036

In my case I did the following:

  1. Uninstall the cli: npm uninstall -g angular-cli
  2. Clear the npm cache: npm cache verify
  3. Reinstall the angular cli: npm install -g @angular/cli@latest

After installing the cli and generating your project, run the following inside the project directory where name is the name of the app as defined inside the angular.json file:

ng add @angular/pwa --project name

It is important to note that name is the name of the project as listed inside the angular.json file. Inside that file you should find a directive like this: "defaultProject": "app",. So in my case it will be:

ng add @angular/pwa --project app

Sign up to request clarification or add additional context in comments.

1 Comment

--project app made my day!
8

I couldn't install the package when I ran the below command

ng add @angular/pwa

When I was trying to fix this issue, The last version of @angular/pwa was v14.2.7 and my angular version was v11 so I installed a lower version of @angular/pwa and it worked for me. I ran this command

 ng add @angular/[email protected]

Comments

5

I try updating the @angular-devkit/schematics-cli package to the latest version:

npm install @angular-devkit/schematics-cli@latest

but that failed. In the end I this worked for me:

I updated the entire @angular/cli package to the latest version:

npm install @angular/cli@latest

I did this on a new ng new project. It used the latest /pwa version enter image description here

enter image description here

1 Comment

I had to update the CLI too in order to get the command to work
0

For existing projects, ng generate won't be the best solution. Check the angular.json in your project folder. "projects" key may have a project name. Underneath that you may find configurations for multiple commands. Try with that project name.

Comments

0

In my case I was changing the name only in the package.json file yet running ng add @angular/pwa --project name checks for the name in the angular.json file. So I had to update the name in the angular.json file as well and everything worked seemlessy then.

Comments

0

Hamid answer worked for me. I am running Angular cli v8.2.2. For installing Angular 14 pwa did not work for me and kept on giving the error "this.tree.readText is not a function". Using the following command and installing [email protected] worked for me:

ng add @angular/pwa@ --project projectNameInAngularJson

2 Comments

I think this answer is more a comment, may you have to less reputation
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
0

it works for me and beware on the project name it must be the same as in your package.json

ng add @angular/pwa --project <project-name>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.