Skatedude
Skatedude

Reputation: 363

How can I import file from another directory that is at the same level?

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

Upvotes: 4

Views: 16849

Answers (2)

Kacper Wiszczuk
Kacper Wiszczuk

Reputation: 2007

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?

Upvotes: 9

Nachshon Schwartz
Nachshon Schwartz

Reputation: 15765

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

import '../Folder2/File2';

Upvotes: 0

Related Questions