Angular-CLI with paths in tsconfig

Hi,

I am working on a project where we are using angular-cli. I am configuring the paths property in the tsconfig.app.json (https://www.typescriptlang.org/docs/handbook/tsconfig-json.html), so that I can import for example like so:

import { MyComponent } from '@modules/core'

instead of

import { MyComponent } from '../../../modules/core' 

when I build with angular (ng serve or ng build) everything works fine. But webstorm doesnt recognize the path and I get an error on the input (and of course no code completion when I type in the import)

Should I configure something ? (I dont want webstorm to do the typescript compilation, as angular-cli is already taking care of that)

 

thank you for your help

Anton

20 comments

Please can you provide your tsconfig.json?

0

Please have a look at this sample project: https://github.com/antonkarsten/sample

The tsconfig file is src/tsconfig.app.json (so it is not the one in the root). This is particular to angular-cli (https://cli.angular.io/)

If you look on line 8 in src/app/app.module.ts you can see how I imported the button component. This only works beause I configured the paths property in the tsconfig.app.json.

when you run the project (with thte command ng serve) everything is fine, but webstorm show it as an error. ( I believe this way of creating an alias is relatively new to Typescript )

thank you for your help in this issue

Anton

0

>The tsconfig file is src/tsconfig.app.json (so it is not the one in the root).

 

sorry, but tsconfig.app.json is not tsconfig.json - it's just a config file internally used by the cli build process. TypeScript compiler doesn't consider such files, only tsconfig.json files are respected. If you run tsc in terminal, you will see the same error:

C:\downloads\sample-master\sample>tsc
src/app/app.module.ts(8,33): error TS2307: Cannot find module '@app'.

 

Adding

"paths": {
"@app": ["app/"]
},

to the tsconfig.json in the root solves the issue

 

0

I'm using Intellij IDEA Ultimate and I'm seeing the same problem.

It shows an error, and provides no code completion.  Here's my tsconfig.json, and below is the IDEA build info.  Note, I had to use 

"@app/*": ["app/*"]

(note the /*) to get ng serve to compile.

{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"baseUrl": ".",
"paths": {
"@app/*": ["app/*"]
},
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}

 

 

IntelliJ IDEA 2017.2.3
Build #IU-172.3968.16, built on August 21, 2017
Licensed to FUJIFILM TeraMedica / Mike Conner
Subscription is active until December 14, 2017
JRE: 1.8.0_152-release-915-b11 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1

 

 

 

1

Running tsc in terminal gives similar result for your mappings:

C:\WebstormProjects\untitled>tsc
app/test.ts(1,25): error TS2307: Cannot find module '@app/services/myService'.

Try changing mappings to

"baseUrl": ".",
"paths": {
"@app/*": [
"app/*"
]
},




0

Yeah, my mistake.  Between when I originally posted it and you responded, I had already seen the error and updated my comment. 

I originally had it as @app/*, based on a stackexchange post, but changed it, per your response to Anton ("Adding "paths": {
"@app": ["app/"]
},... to the tsconfig.json in the root solves the issue, and didn't realize that it broke the build before I first posted.   

Anyway, even after fixing it (switching back to "@app/*": "app/*"), I was still getting a problem.   

However, based on another post, I saw that someone was using "src for baseUrl:  

"baseUrl": "./src",

And appears to work.  I'm guessing you don't have the problem because app is at the root of your project, for some reason.  Looking at your screencap, the tooltip says:

C:\WebstormProjects\untitiled\app\service\...

Where untitled is presumably your project.  But a project I generated with angular-cli is of the form:

myproject\src\app

That is, with a src in between.  So app/*  would not match anything. I'm guessing I could have instead changed the path mapping to "src/app/*", and not changed the baseUrl, too.  Haven't tried it, yet.... OK tried it, that didn't work. Don't get it.  Oh well, in any case, the baseUrl:src seems to do the trick.

 

 

2

I have the same issue it would be nice to have a commonly applicable solution that works both with angular-cli, IntelliJ and Typescript.

Is there an issue for this problem in YouTrack that I can follow?

0

What issue, sorry? No bugs are reported in this thread, so there is nothing to track

0

Hi, I have same problems on Mac OS.

My tsconfig.json (root):

{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
],
"paths": {
"@api/*": ["app/api/*"]
}
}
}

The WebPack success compile and application correct work if I import the client as:

import {ApiClient} from '@api/api-client';

But the PhpStorm write the error

Cannot resolve directory '@api'

 

0

You don't have "baseUrl" specified in your tsconfig.json. And "paths" are resolved relative to "baseUrl", so this property is required.

See https://www.typescriptlang.org/docs/handbook/module-resolution.html

Note that passing your tsconfig.json to tsc in terminal results in

tsconfig.json(18,5): error TS5060: Option 'paths' cannot be used without specifying '--baseUrl' option.

error.

Adding 

"baseUrl": "."

to your tsconfig.json should solve the issue:

 

0

@Elena Pogorelova, thank. All works!

0

Hi,

I have a project created using the Ionic CLI: https://github.com/Robinyo/big-top

 

I have configured 'compilerOptions' in 'tsconfig.json':

"compilerOptions": {

...

"target": "es5",
"typeRoots": [
"../node_modules/@types"
],
"baseUrl": "./src",
"paths": {
"@app/*": [ "app/*" ],
"@assets/*": [ "assets/*" ],
"@env": [ "environments/environment" ],
"@pages/*": [ "pages/*" ],
"@services/*": [ "services/*" ],
"@theme/*": [ "theme/*" ]
}
}

...

Everything works as expected (e.g., npm run dev, npm run build, npm run test, etc.) except in WebStorm 2017.2 I receive the following error for '*.spec.ts' files:

 

All other '*.ts' file are fine:

Cheers
Rob

0

Hi,

Just noticed that if I remove "**/*.spec.ts" from 'exclude' the error goes away:

"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"src/test.ts",
"e2e"
]

Does WebStorm support a 'tsconfig.spec.json' ?

Cheers
Rob

 

0

>Just noticed that if I remove "**/*.spec.ts" from 'exclude' the error goes away

 

as the file is excluded from tsconfig.json, path mappings specified there are not applied to it - this is the expected behavior

 

>Does WebStorm support a 'tsconfig.spec.json' ?

 

No, it doesn't. TypeScript compiler service doesn't consider such files, only tsconfig.json files are respected.

0

Hello, 

Same problem for me. 

My tsconfig.json: 

 

And in my component.ts:

The error is :

Angular: Can't resolve all parameters for myComponent.

 

Someone can help me ? :)

Thank you !

0

And your problem is also different. Guys, would you be so kind to create new threads for new issues/queries? Instead of adding comments to age-old threads, commenting on issues that have almost nothing in common with yours?

Your error is thrown by Angular language service, not by the IDE itself. Looks related to https://github.com/angular/angular/issues/18484 and https://github.com/angular/angular/issues/16382
Try disabling the service (in Settings | Languages & Frameworks | TypeScript, clear the Angular language service checkbox) - does the problem persist?

1

Sorry, I'll open a new topic next time!

It works well with the change.

Thank you very much Elena

0

I have the same issue (Webstorm 2018.2.5, Ubuntu 18.04.1 LTS).
Angular language service is turned off. Running ng build works fine, but I have an error in my editor.
My tsconfig.json:

I have the following project structure:
--lab5
    --backend
        --api.ts
    --frontend
        --tsconfig.json (baseUrl:"..", paths: { "@backend/*": ["backend/*"] })
        --src
            --app
                --users
                    --users.component.ts (import { UsersAjaxAPI} from '@backend/api')

Webstorm still reports me an error, though it provides me a link and opens me the correct file if I press Ctrl+Mouse1


0

Similar configuration works fine for me:

 

 

BTW, what is a result of running tsc -p . in frontend directory? Do you have any other tsconfig.json files in your project?

Sample project that shows up the issue would be helpful

0

It's a miracle, but yesterday I left my project with that error untouched. Today everything works fine, no errors are reported. I can't reproduce that issue, no matter what I do, though I still have to disable Angular language service.

1

Please sign in to leave a comment.