You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to configure things so I don't have to use ugly (and brittle) relative paths when including modules from my own project. I modified tsconfig.json to include:
ERROR in ./src/app/content/content.component.ts
Module build failed: Error: /Users/lehresman/Projects/gazelle-ui/src/app/content/content.component.ts (2,29): Cannot find module '@app/models/client.model'.)
at _checkDiagnostics (/Users/lehresman/Projects/gazelle-ui/node_modules/@ngtools/webpack/src/loader.js:116:15)
at /Users/lehresman/Projects/gazelle-ui/node_modules/@ngtools/webpack/src/loader.js:141:17
@ ./src/app/app.module.ts 17:0-65
@ ./src/main.ts
@ multi main
I've discovered that Webpack has configuration options for this, but I can't figure out how to configure Webpack through Angular-CLI to support this. Is this a bug, or is this feature not exposed?
Well, it should work then. I'am use the same config. (But with beta.22 version. Still have not time for update.)
Make shure, that you restart your ng serve after changing tsconfig.json
It will not load new settings while serving.
Thanks for letting me know that it was supported. I was chasing rabbit trails trying to get angular-cli to support it, but turns out it IS supported already. I simply had my paths wrong.
Also, for posterity, I had to make the paths be an array. It looks like Typescript accepts a string, but angular-cli expects it to be an array.
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@app/*": ["app/*"] // <-- must be an array
},
...
Reacted by AntoineC, Danny Mayer, Renato Araujo Carneiro, huangxiuqi, idemanm, Michel Parpaillon, David Enke, Li Ming, Edmundo Santos, Shawn Pivonka and 4 more
Sorry for commenting here, but this isn't working for me. I added baseUrl and paths to tsconfig.json just like the OP posted, but ng build says the module can't be found (it works in the editor, so I guess it's properly configured).
@Parziphal I don't know what's going on in your setup, but if you can reproduce in a new project then I ask you to open a new issue with the reproduction. If it does work there, then have a look at how all the tsconfig.json might differ from yours.
and then in my files I referenced packages in local_modules using the @local/X alias. That worked in 1.2.3. I just upgraded to 1.3.0, and it fails. Reverting to 1.2.3 resolves it. I'm not sure if this was supposed to work, or if this was just a hack I found in the comments.
Reacted by Lulus Young, Jayson Harshbarger, 0zkr1 and Denes Papp
that does work great since your mapping directly to individual files, but may still fail when using the * wildcard.
After reading the Module Resolution page, I ran tsc --traceResolution
'baseUrl' option is set to 'C:/Users/WindowsUsers/source/repos/path-alias-test/src', using this value to resolve non-relative module name '@services'.
'paths' option is specified, looking for a pattern to match module name '@services'.
Module name '@services', matched pattern '@services'.
Trying substitution 'app/services/*', candidate module location: 'app/services/*'.
Loading module as file / folder, candidate module location 'C:/Users/WindowsUsers/source/repos/path-alias-test/src/app/services/*', target file type 'JavaScript'.
File 'C:/Users/WindowsUsers/source/repos/path-alias-test/src/app/services/*.js' does not exist.
File 'C:/Users/WindowsUsers/source/repos/path-alias-test/src/app/services/*.jsx' does not exist.
Directory 'C:/Users/WindowsUsers/source/repos/path-alias-test/src/app/services/*' does not exist, skipping all lookups in it.
Loading module '@services' from 'node_modules' folder, target file type 'JavaScript'.
Directory 'C:/Users/WindowsUsers/source/repos/path-alias-test/src/app/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/Users/WindowsUsers/source/repos/path-alias-test/src/node_modules' does not exist, skipping all lookups in it.
File 'C:/Users/WindowsUsers/source/repos/path-alias-test/node_modules/@services.js' does not exist.
File 'C:/Users/WindowsUsers/source/repos/path-alias-test/node_modules/@services.jsx' does not exist.
Directory 'C:/Users/WindowsUsers/source/repos/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/Users/WindowsUsers/source/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/Users/WindowsUsers/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/Users/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
======== Module name '@services' was not resolved. ========
I was able to fix it finally by setting my tsconfig (tsconfig.app.json has no baseUrl or Paths because it only extends tsconfig)
I'm having a similar problem after updating to angular 7.0.4. I do not use angular cli for my project, but I am convinced it is ngtools/webpack causing the problem.
The relevant portions of my tsconfig look like this:
Circular dependency errors for any service which imports another service, since the both resolve to @services/index`
All top level imports have to use @services/* instead of just @services/
I've run tsc -p ./tsconfig.app.json --traceResolution a couple dozen times and grepped for the paths which were throwing errors in the angular build, and all of them are resolving to the right file through tsc.
I'm not sure why this changed between 6 and 7, since there was no mention of any changes around tsconfig in any of the Changelogs. Anyone else still stuck after following the advice in this thread?
With the same tsconfig.json I posted above, I was able to get it to build, with no circular dependencies and no stars in the path. I did a global Find and replace for all imports ending in / and replaced them all with /index. This should be default behavior, and tsc resolves without the index, but by adding it, angular compiles fine.
I'm not super happy with this resolution, but once the Angular team fixes this issue, it should be pretty easy to do another find and replace to remove the /index in every file.
I commented out baseUrl property in tsconfig.app.json and now everything works including intellisense and ng serve.
Previously intellisense worked but ng serve failed with "Cannot find module" errors.
I commented out baseUrl property in tsconfig.app.json and now everything works including intellisense and ng serve.
Previously intellisense worked but ng serve failed with "Cannot find module" errors.
Activity
ValeryVS commentedon Jan 3, 2017
Are you using
ng servewith--aotflag?Probably, tsginfig's
pathsis ofiicialy supported by angular cli only for JIT compile for now.lehresman commentedon Jan 3, 2017
No, just plain
ng serveValeryVS commentedon Jan 3, 2017
Well, it should work then. I'am use the same config. (But with beta.22 version. Still have not time for update.)
Make shure, that you restart your
ng serveafter changing tsconfig.jsonIt will not load new settings while serving.
lehresman commentedon Jan 3, 2017
Thanks for letting me know that it was supported. I was chasing rabbit trails trying to get angular-cli to support it, but turns out it IS supported already. I simply had my paths wrong.
lehresman commentedon Jan 3, 2017
Also, for posterity, I had to make the paths be an array. It looks like Typescript accepts a string, but angular-cli expects it to be an array.
lehresman commentedon Jan 3, 2017
Ok, that works fine for the main app, but my tests don't seem to recognize it.
ng testshows this error:ng serveis working fine.Here is the
dashboard.component.tsAre the
tsconfnig.jsonbaseUrlandpathssettings not supported forng test?dzonatan commentedon Jan 4, 2017
Yes, tests have that issue.
See #3586 issue. PR #3831 should fix it.
filipesilva commentedon Jan 24, 2017
Fixed by #3831, thanks @dzonatan !
Parziphal commentedon Aug 5, 2017
Sorry for commenting here, but this isn't working for me. I added baseUrl and paths to tsconfig.json just like the OP posted, but
ng buildsays the module can't be found (it works in the editor, so I guess it's properly configured).ng -vsays:filipesilva commentedon Aug 10, 2017
@Parziphal I don't know what's going on in your setup, but if you can reproduce in a new project then I ask you to open a new issue with the reproduction. If it does work there, then have a look at how all the tsconfig.json might differ from yours.
duncanwerner commentedon Aug 14, 2017
I believe this regressed. I had the following setup in [root]/tsconfig.json:
and then in my files I referenced packages in
local_modulesusing the@local/Xalias. That worked in 1.2.3. I just upgraded to 1.3.0, and it fails. Reverting to 1.2.3 resolves it. I'm not sure if this was supposed to work, or if this was just a hack I found in the comments.ZiruZanrgeiff commentedon Aug 14, 2017
Hi, I have the same problem. I am in a fresh project. With this setup:
And after set the paths:
I try to run
ng servean I got this error:22 remaining items
seabass223 commentedon Nov 8, 2018
thanks for the response. I tried
"@services*":["app/services*"]but that didn't work either. I'll give the module-resolution handbook a good read.MarcosMeli commentedon Nov 11, 2018
Solved the problem in Angular 7 adding the paths to both
tsconfigfilestsconfig.jsonhelps vscode to works and with intellisensetsconfig.app.jsonis also needed for angulartsconfig.json
tsconfig.app.json
and the intellisense works great
seabass223 commentedon Nov 12, 2018
that does work great since your mapping directly to individual files, but may still fail when using the
*wildcard.After reading the Module Resolution page, I ran
tsc --traceResolutionI was able to fix it finally by setting my tsconfig (tsconfig.app.json has no baseUrl or Paths because it only extends tsconfig)
and appropriately using index.ts files just like node_modules/@angular does.
The
*were the culprit.localyost3000 commentedon Nov 16, 2018
I'm having a similar problem after updating to angular 7.0.4. I do not use angular cli for my project, but I am convinced it is ngtools/webpack causing the problem.
The relevant portions of my tsconfig look like this:
This builds but with 2 big caveats.
@services/index`@services/*instead of just@services/I've run
tsc -p ./tsconfig.app.json --traceResolutiona couple dozen times and grepped for the paths which were throwing errors in the angular build, and all of them are resolving to the right file throughtsc.I'm not sure why this changed between 6 and 7, since there was no mention of any changes around tsconfig in any of the Changelogs. Anyone else still stuck after following the advice in this thread?
MarcosMeli commentedon Nov 16, 2018
@seabass223 it works for me with
*wilcard with no problems but only if I duplicate paths intsconfig.jsonandtsconfig.app.jsonand the imports works fantastic
localyost3000 commentedon Nov 16, 2018
Super quick update:
With the same
tsconfig.jsonI posted above, I was able to get it to build, with no circular dependencies and no stars in the path. I did a global Find and replace for all imports ending in/and replaced them all with/index. This should be default behavior, andtscresolves without the index, but by adding it, angular compiles fine.I'm not super happy with this resolution, but once the Angular team fixes this issue, it should be pretty easy to do another find and replace to remove the
/indexin every file.luckylooke commentedon Dec 10, 2018
@seabass223 maybe this is the correct docs for CompilerOptions --> https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
gogakoreli commentedon Jan 16, 2019
In my case
tsconfig.spec.jsonwas overwritingbaseUrlanilvangari2005 commentedon Feb 8, 2019
I commented out
baseUrlproperty intsconfig.app.jsonand now everything works including intellisense andng serve.Previously intellisense worked but
ng servefailed with "Cannot find module" errors.tsconfig.json:tsconfig.app.jsonrelevant configanikets43 commentedon Feb 24, 2019
@ivanguimam Did you find solution for this?
danielpcampagna commentedon Feb 27, 2019
@Bengreen I have the same problem. Did you find the solution?
skwny commentedon Jul 23, 2019
Came here b/c I just experienced this same issue. I pieced together a work around from @swimmadude66 solution. For anyone else:
tsconfig.json compiler options looks like this:
When importing a service anywhere, I do this:
index.tsis located in theservicesdir, and contains:There are no more circular dependency warnings after this.
veerabhadraraochikka commentedon Jul 31, 2019
while migration from mono app to multi app we should look at baseurl in tsconfig.json
angular-automatic-lock-bot commentedon Sep 9, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.