angular-cli: tsconfig "paths" config is not recognized
Mac OSX Sierra angular-cli version 1.0.0-beta.24
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:
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@app/*": "app/*"
},
...
And that works great for TypeScript.
However, “ng serve” is throwing this error:
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?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 48 (7 by maintainers)
I seems like the error was that
tsconfig.app.json
was overwriting thebaseUrl
option of the root-leveltsconfig.json
, setting it to./
(instead of just.
). Once I removed that option from that file, the build was successful.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.
I have the same problem as @ivanguimam mentioned, has anyone solved this? My app works fine but my e2e tests crashing every time when I try to start them with path variables.
is this broken for anybody in Angular 7? I don’t see paths anymore in https://angular.io/api/core/CompilerOptions I’m getting compilation errors with ng serve on all of the paths that I had in tsconfig.json
@duncanwerner I used your idea and got it to work on Mac. Seems like the builder uses
tsconfig.app
and linter(?) usestsconfig
tsconfig:
tsconfig.app
Solved the problem in Angular 7 adding the paths to both
tsconfig
filestsconfig.json
helps vscode to works and with intellisensetsconfig.app.json
is also needed for angulartsconfig.json
tsconfig.app.json
and the intellisense works great
I have a problem similar to this when I run the tests E2E
Angular CLI version:
tsconfig.json
tsconfig.e2e.json
Error:
Error: Error: Cannot find module '@e2e/utils/utils'
vscode (or presumably any other editor, but that’s what I use as well) uses the root
tsconfig.json
. so make sure that aliases in that file are correct relative to the location of that file. you may also have to close/open files or even the whole editor to get it to refresh the cache.I believe this regressed. I had the following setup in [root]/tsconfig.json:
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.@Bengreen I have the same problem. Did you find the solution?
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 rantsc --traceResolution
I 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.I still haven’t figured this out completely, but I have a workaround. This may be a windows-specific problem. This does not work:
This does work (baseURL moves up one directory, paths moves down):
Hope that helps someone. I may try to track down the specific problem, but it’s not a typescript problem; trying a generic
tsc
compile with a different config, both of these work.In my case
tsconfig.spec.json
was overwritingbaseUrl
Yeah, I mean the error was that I wasn’t aware of the overwriting, not that “the file is overwriting my config, that’s an error, how dare that file!”.
Now I just found a little thing. The IDE (vscode) doesn’t show any suggestions while I type a path using the alias (so it won’t autocomplete, I have to write the full path). Is there a way to fix this? Not that it’s too important but… if it can be fixed it’d be nice.
I commented out
baseUrl
property intsconfig.app.json
and now everything works including intellisense andng serve
. Previously intellisense worked butng serve
failed with “Cannot find module” errors.tsconfig.json
:tsconfig.app.json
relevant configI think I’ve got it fixed now
Setup
The above compiles fine with no errors.
When we add barrel files in order to simplify our imports
src/app/service/index.ts
src/app/service/sub/index.ts
and change app.component.ts imports to
Resolution
If we remove the final * in tsconfig.json paths
["app/services/*"]
to["app/services/"]
for concise imports to work with barrel(index.ts) files.I am having trouble getting paths to work when building packages. ie in the tsconfig.lib.json I have added:
to tsconfig.lib.json and then try to utilise it with a path such as
the target file is
src/lib/bids/crossfilter-data/crossfilter-data.component.ts
Sorry, I misunderstood. It does not autocomplete aliased paths. File a feature request over at the vscode repo.
Maybe related to #7341, although for me
--aot
does not work either.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.