I encountered a similar issue when attempting to transition a project from Angular 10 to Angular 11. I tried copying the ./src/app
folder, but unfortunately, that approach did not yield the desired results...
However, in a fresh project setup, I managed to make path aliasing work by following these steps:
- Start by updating your Angular CLI:
PS C:\Projects> npm uninstall --g @angular/cli
PS C:\Projects> npm i --g @angular/cli
- Create a new project and enable strict mode setting to
true
:
PS C:\Projects> ng new <<name-project>>
PS C:\Projects> cd <<name-project>>
- After the CLI finishes, make the following modifications to
tsconfig.app.json
:
/* More details on this file are available at: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": [],
// INSERT BELOW ↓↓↓
"baseUrl": "./",
"paths": {
"@tool/*": [ "src/app/tool/*" ],
"@models/*": [ "src/app/models/*" ],
"@services/*": [ "src/app/services/*" ],
"@components/*": [ "src/app/components/*" ],
"@interfaces/*": [ "src/app/interfaces/*" ]
}
// INSERT ABOVE ↑↑↑
},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.d.ts"
]
}
- Similarly, update
tsconfig.json
with the following configurations:
/* For more information on this file, refer to: https://angular.io/config/tsconfig. */
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"module": "es2020",
"lib": [
"es2018",
"dom"
],
// ADD THIS ↓↓↓
"baseUrl": "./",
"paths": {
"@tool/*": [ "src/app/tool/*" ],
"@models/*": [ "src/app/models/*" ],
"@services/*": [ "src/app/services/*" ],
"@components/*": [ "src/app/components/*" ],
"@interfaces/*": [ "src/app/interfaces/*" ]
}
// ADD THIS ↑↑↑
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}
- Finally, adjust
tsconfig.spec.json
as shown below:
/* Further details on this file available at: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": [
"jasmine"
],
"baseUrl": "./",
"paths": {
"@tool/*": [ "src/app/tool/*" ],
"@models/*": [ "src/app/models/*" ],
"@services/*": [ "src/app/services/*" ],
"@components/*": [ "src/app/components/*" ],
"@interfaces/*": [ "src/app/interfaces/*" ]
}
},
"files"quot;: [
"src/test.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}