Can't resolve 'fetch'

Hello, I am using embroider with ember 4.5.

I have installed ember-fetch and my import is, import fetch from "fetch". I get the following error:

Module not found: Error: Can't resolve 'fetch' in '$TMPDIR/embroider/f8be1a/app/ember-kb-base/ember-kb-base/dist/authenticators/oidc.js'

Just to note, ember-kb-base is an addon. I see lots of posts about embroider and ember-fetch, but none of them help so far.

I notice ember-fetch is written in typescript. Do I need to add anything to the WebPack config or rollup.config.js?

Just to sanity check is ember-fetch a dep or devDep? It would need to be a dep iirc. Is this a v2 addon or still v1 format?

I’m just starting to play around with Embroider myself so I probably won’t be a ton of help but some others may be able to weigh in.

Thank you… I have tried it in both places, but it still doesn’t work if I put it in dependencies… The addon is in v2 format. I copied an existing v2 addon (ember-safe-button) and built it up from there.

I am new to this as well, so not sure how the code gets built (with rollup etc).

I think ember-fetch needs to change their recommended import for fetch, because it uses whatwg-fetch: ember-fetch/package.json at master · ember-cli/ember-fetch · GitHub

which is imported from whatwg-fetch, not fetch: whatwg-fetch - npm

which is probably why the v2 addon “isn’t working”. It’s revealing that ember-fetch has been using hax for some time, and those need to be fixed. (those hax don’t apply in a v2-addon environment, as we must follow ESM, strictly – which doesn’t allow package-renaming)

2 Likes

Thank you for your help. This is probably a stupid question, but can I just replace ember-fetch with whatwg-fetch? I did the following:

# Added "whatwg-fetch" to "dependencies"
  "dependencies": {
    "whatwg-fetch": "^3.6.2"
  },

# Then the import:
import 'whatwg-fetch'

And it seems to work. Am I missing something?

Depends on purpose i think. If using fastboot, I’m not sure what the path forward is, other than what is described above – if things work is fastboot, I’m surprised.

If you aren’t using fastboot, why not use global fetch? ( Don’t import anything?)

2 Likes

Thank you. We are not using fastboot. I didn’t think about using global fetch. I just tried and it works perfectly. Thank you so much for your help.

1 Like