3

How to import stuff from a file that is in another directory but at the same level? For example: i am in file 1 of folder 1 and want to import stuff from file 2 from folder 2. How to do that? Currently i get the error that the module cannot be found.

Current tree:

-ComponentsFolder
    -Folder1
       -File1
    -Folder2 
       -File2
Liam
  • 27,717
  • 28
  • 128
  • 190
Skatedude
  • 343
  • 3
  • 4
  • 12

2 Answers2

8

You simply construct relative path from one file to another. Like in in OS.

import '../Folder2/File2';

But you should avoid relative imports because it might get painful really quickly. Consider this: ../../../../dir1/dir2/dir3. Pretty bad, isn't it? There is really good tutorial how to avoid relative paths and use absolute paths. Just a few configuration changes and you get dir1/dir2/dir3 How to avoid imports with very long relative paths in Angular 2?

Kacper Wiszczuk
  • 1,809
  • 11
  • 14
0

Simply move back one folder and then go into the folder, so in file 1 get it like this:

import '../Folder2/File2';
Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98