schematics ng-add run as externalSchematic throws error "TypeError: Cannot read property 'projectType' of undefined" because projects in angular.json are not read
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x ] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:
Current behavior
I am trying to run the ng-add
schematic in one of my schematics:
externalSchematic('@ngneat/transloco', 'ng-add', {
// passing my config here...
langs: languages.join(', '),
path: 'assets/',
}),
When I run it, I get the following error:
TypeError: Cannot read property 'projectType' of undefined
at /Users/laurens/git/sandbox/node_modules/@ngneat/transloco/schematics/ng-add/index.js:91:31
So, this line is failing: https://github.com/ngneat/transloco/blob/master/schematics/src/ng-add/index.ts#L122
Because there are no projects found: https://github.com/ngneat/transloco/blob/master/schematics/src/utils/projects.ts#L31
If I put a console.log(config)
here, I do indeed see the projects are missing: https://github.com/ngneat/transloco/blob/master/schematics/src/utils/projects.ts#L18
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 2,
"newProjectRoot": "projects",
"projects": {},
"defaultProject": "sandbox"
}
However, my angular.json has projects, it’s just a default config generated by the CLI:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"sandbox": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/sandbox",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "sandbox:build"
},
"configurations": {
"production": {
"browserTarget": "sandbox:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "sandbox:build"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "sandbox:serve"
},
"configurations": {
"production": {
"devServerTarget": "sandbox:serve:production"
}
}
},
"cypress-run": {
"builder": "@briebug/cypress-schematic:cypress",
"options": {
"devServerTarget": "sandbox:serve"
},
"configurations": {
"production": {
"devServerTarget": "sandbox:serve:production"
}
}
},
"cypress-open": {
"builder": "@briebug/cypress-schematic:cypress",
"options": {
"devServerTarget": "sandbox:serve",
"watch": true,
"headless": false
},
"configurations": {
"production": {
"devServerTarget": "sandbox:serve:production"
}
}
}
}
}
},
"defaultProject": "sandbox"
}
Why does it not read out the projects?
Expected behavior
I expect it to run the ng-add schematic
Minimal reproduction of the problem with instructions
See above description. Simply run the @ngneat/transloco:ng-add schematic using the externalSchematic
command in a custom schematic, in an Angular app.
What is the motivation / use case for changing the behavior?
Running the Transloco schematic from schematics.
Environment
Angular version: 9.1.7
For Tooling issues:
- Node version: 12.16.3
- Platform: Mac
Others:
"@angular-devkit/build-angular": "~0.901.6",
"@angular-devkit/schematics": "^9.1.7",
"@schematics/angular": "^9.1.7",
Issue Analytics
- State:
- Created 3 years ago
- Comments:9
Found a solution, something similar sometimes happens for components generation and you need to set the project using
--project={your-project-name}
So the full solution will be:
ng add @ngneat/transloco --project={your-project-name}
I’m having this same issue and in my
angular.json
I have defined thatprojectType
what should I do?