3

I am trying to use import instead of require for all modules in my project, but some older npm modules only have instructions for require.

In the case of 'isomorphic-fetch' I can't find the proper way to use import:

works

require('isomorphic-fetch')

fails

import 'isomporphic-fetch'   
import Something from 'isomorphic-fetch'

// error Can't resolve 'isomporphic-fetch' from Project/src/js/

Converting to import does work with the es6-promise module.

works

require('es6-promise').polyfill()

works

import Something from 'es6-promise'
Something.polyfill()
2

1 Answer 1

4

Since import does work with other modules, and require('isomorphic-fetch') works, it's probably a named export problem.

Try import * as Something from 'isomorphic-fetch'

If that works, it's because isomorphic-fetch does not do export deafult so you have to pull in the imports by name, or use the notation I wrote above. Take a look at the MDN link I put on top.

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

3 Comments

Thanks, that was it. Still not sure why I need Something, since that variable is never used. import * from 'isomorphic-fetch' doesn't work.
Looking at the repo, it's only export is the fetch function. But in addition to exporting it, he overrides the global as well. So as he writes in the README you don't need to import at all, just require without assigning. I agree that es6-promise's polyfill API is more easily understandable
Yes, I realise that just require also works, but I would prefer sticking to one syntax. It would be great if just import 'isomporphic-fetch' worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.