3

In order to avoid relative path when I import file, I try to configure angular to understand path.

So far, it's not working at all, my code :

//tsconfig.app.json
"compilerOptions":{
    //lot of things . If it matter moduleResultion is set to node
    "baseUrl": ".",
    "paths": {
        "@modules": ["app/modules/"]
        ...
    },
}



//in a component: 
import { X } from '@modules/Y/component/index'

when running ng serve , the console output an error about : : Cannot find module '@modules/Y/component/index'.

This is definitly working with relative path like import { X } from ../../../modules/Y/component/index

So, I expect my tsconfig.app.json or tsconfig.json ( or maybe both) are wrong, however, I can't find any good tutorial about how to do it correctly for an angular app.

Currently using angular 4 with basic associate tools (typescript 2.3.3, angular-cli 1.0.6 and provide webpack)

Can you point out the problem to me, or give me a good doc/tuto that resolve this issue with angular please ? All the answer I have seen so far on SO or github issue didn't work at all.

NOTE : The architecture looks like this

project
   |
   -tsconfig.json //had try things on this one too but does nothing.
   -src/
     |
     -tsconfig.app.json
     -app/
       |
       -modules
       -othersFolder
8
  • How did you set your project up? ng new normally sets things up ok and you're good to go.... Commented Jul 5, 2017 at 15:41
  • I did set it up with ng-cli while angular was still on RC2. Commented Jul 5, 2017 at 15:44
  • If you're using something like VSCode, if you right click on the class in the import statement you can navigate into the source with Goto Definition. And often time editors do something like put squiggly line under the path if you've got your relative paths incorrect. I'd be checking that then. Commented Jul 5, 2017 at 15:55
  • I'd also be checking you've setup app-modules.ts correctly. angular.io/guide/ngmodule Commented Jul 5, 2017 at 15:58
  • I'm on ST. the goto is working fine, Still not working in the console so must be something else. Commented Jul 5, 2017 at 15:59

1 Answer 1

8
+50

Your code should work with

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

Read more about module resolution in typescript here

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

Sign up to request clarification or add additional context in comments.

2 Comments

can't give bounty until 19h.
The difference (to the first code sample in the question) is to use /* after the module name? ("paths" is still a child of compiler options?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.