When I try to get angular/cli to generate a new component for me all it does is print the message
this.tree.readText is not a function
and exits.
I am using Ubuntu 22.04 and using ng
in a console window.
I am trying to generate a new component in a feature module. I am starting ng
in the feature-module directory with the following command:
ng g c components/my-new-component --change-detection OnPush
The working directory is similar to:
~/Develop/my-project/src/app/feature-module
and I’m expecting four new files to be generated in the new folder
~/Develop/my-project/src/app/feature-module/components/my-new-component/
as well as the module file
~/Develop/my-project/src/app/feature-module/feature-module.module.ts
to be updated with the newly generated component.
I’m also getting this behavior on Ubunut 20.04.
A colleague has the same problem on his Mac (an M1) when using ng
on the command line, but is not really hampered as he uses IntelliJ and has an extension for generateing new components, rather than using the command line.
I’ve tried googling the problem without any success. I’ve also tried deleting node_modules along with the package-lock.json file and reinstalling all packages but to no avail.
I’m using the following node/typescript/angular versions:
_ _ ____ _ ___
/ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ | '_ / _` | | | | |/ _` | '__| | | | | | |
/ ___ | | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ __| |_|__, |__,_|_|__,_|_| ____|_____|___|
|___/
Angular CLI: 13.3.8
Node: 14.19.2
Package Manager: npm 6.14.17
OS: linux x64
Angular: 13.3.11
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1303.8
@angular-devkit/build-angular 13.3.8
@angular-devkit/core 14.0.3
@angular-devkit/schematics 14.0.3
@angular/cdk 13.3.9
@angular/cli 13.3.8
@angular/material 13.3.9
@schematics/angular 14.0.3
rxjs 6.6.7
typescript 4.5.5
Does anyone have any idea what I’m doing wrong?
UPDATE
I tried creating a new project, adding a feature module and adding a component to the feature module – everything worked perfectly, so I suppose the problem is being caused by something in the project (or its structure) I’m working on. This is going to be fun…
7
Answers
ng was exiting unexpectedly with only the message
so I went looking for
this.tree.readText
in the code. It seems only to be used in the @schematics package in conjuction with reading and writing the workspace. Following the suggestion by @Vega that it might be a version incompatibility issue I checked what was inangular.json
against the online documentation - during the project we've continually upgraded from version 8 to 13.There was a difference: at the end of the file was the following:
This was added during the migration from TSLint to ESLint. We used the angular-eslint schematic to carry out the migration:
but I'm not sure anymore if this line was added automatically or whether we added the line afterwards.
In the online documentation for the Angular Workspace Configuration (cli options) there's no mention of
defaultCollection
, but there is aschematicCollections
. By replacing the cli-options with this:ng generate
started working again. Interestingly enough the JSON-schema for theangular.json
file specifies the propertydefaultCollection
but notschematicCollections
.Thanks!
Your solution worked to me too. But in this case for my was in:
Bonjour, so I had the same issue as you has in my Ionic Angular Project and looked for days without finding a clear answer to my problem.
However your answer lead me to my solution.
For me my
angular.json
was also configured like thisand it didn’t allow me to change it to
schematicCollections
.The solution for me ended up being downgrading my
@ionic/angular-toolkit
from version 7.0.0 to 6.1.0.I had an older copy of the angular cli installed in my local project (13.0.1) that was overriding the global version (14.2.5). Removed the local project version and worked.
`
`
try reemplace angular.json work for my, try and diff for check diferences and analize this json
In my case needed only update @angular/cli package in project as others angular packages
This problem happened to me when I tried to add service workers to my app using Angular’s Progressive Web App (PWA) capability. I naïvely ran
ng add @angular/pwa
, but my global Angular CLI version was newer than my project’s Angular version. That means the@angular/pwa
version was too new for my Angular project, which caused the error message that this question describes.Here’s how I solved it:
npm remove @angular/pwa
.ng add
again for the correct version. For example, runnpm add @angular/[email protected]
for an old Angular 8 project.