89

I have these two import statements in file:

import Data from 'component/Data.js';
import Data from 'actions/Data.js';

Both files contain a class named Data.

How can I specify which is which? How can I avoid name clash?

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304

4 Answers4

226

Presumably component/Data and actions/Data both have default exports rather than named exports? Like this:

export default class Data {}

If that's the case, then the importer can call the variables whatever they like:

import Data1 from 'component/Data.js';
import Data2 from 'actions/Data.js';

If they are named exports:

export class Data {}

Then you need to use braces along with as to specify the source and target names:

import { Data as Data1 } from 'component/Data.js';
import { Data as Data2 } from 'actions/Data.js';
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • 3
    exactly what I was looking for, I tried `import { Data } as Data2 from 'actions/Data.js';` which didn't work and came here and realized my mistake. – DarkMukke Aug 24 '17 at 12:39
  • note that you must alias both, just aliasing one will not work. I'm not sure why. – DiamondDrake Jan 05 '19 at 06:08
  • Very nice, as I had classes exported with the default modifier, I just can call them whatever I want, didn't know that. – Codigo Morsa Apr 13 '22 at 13:14
14

EDITED: As per RGraham answer, updating my answer:

Can't you import it like this:

import {Data as D1} from 'component/Data.js';
import {Data as D2} from 'actions/Data.js';

Then use it as you require:

D1.{}
D2.{}

referenced from: https://github.com/lukehoban/es6features/blob/master/README.md/#user-content-modules

Savaratkar
  • 1,974
  • 1
  • 24
  • 44
3

There are two alternatives:

import {MyClass as c1} from ......
import {MyClass as c2} from ......

or second alternative:

import MyClass from .......
import {MyClass as c2} from ......

You can use first line but in some cases, you may get stuck using the first line.Then use the second line.

Apurba A
  • 71
  • 6
-1

if using

import { Data as Data1 } from 'component/Data.js';
import { Data as Data2 } from 'actions/Data.js';

if this does work saying Data is not exported, you should clear the last path for example use

import { Data as Data1 } from 'component';
import { Data as Data2 } from 'actions';

instead of

import { Data as Data1 } from 'component/Data.js';
import { Data as Data2 } from 'actions/Data.js';
Nat
  • 135
  • 4
  • That would depend on the `index.js` files in those directories. It's much more likely that you need to use `import { default as Data1 } from 'component/Data.js';` or `import Data1 from 'component/Data.js';` if `Data as …` doesn't work. – Bergi Jun 13 '22 at 01:13
  • yes it really depends, i said if this still didnt work out for you because of how theose classes/functions exported from that directory/file. because those been answered and you might still get error from the right answers from above which is "data is not exported" if that happened you shouldnt hard code it because it might be exported as multiple. wasnt my text clear or what should i change it too if you understand me? – Nat Jun 24 '22 at 05:36
  • "*you might still get error from the right answers from above*" - no. CodingIntrigue's answer is comprehensive and covers all (two) cases. You should not need to make guesses abut `index.js`, "*you should clear the last path*" is bad advice. – Bergi Jun 24 '22 at 15:35
  • That is right, it is bad advice, and I don't know how to make it right to describe all cases because I had that issue before. If you have the right way of explaining all use cases please leave here a comment so some visitors can see it one day. also thank you. – Nat Jun 25 '22 at 03:53
  • I have nothing to add to CodingIntrigue's answer so I won't post my own. What issue did you have with it? What did your export declaration look like, and what import declaration did fail you? – Bergi Jun 25 '22 at 04:03