Data: cannot find the `fetch` module or the `fetch` global when using with fastboot

Created on 14 Aug 2019  ·  10Comments  ·  Source: emberjs/data

Howdy folks, it's me again 👋

This issue is essentially the same problem as https://github.com/emberjs/data/issues/5998 but with a different error message so I thought I would create a new issue.

Essentially adding ember-cli-fastboot to a newly generated Ember app doesn't work and here is the error we get this time:

Error: cannot find the `fetch` module or the `fetch` global. Did you mean to install the `ember-fetch` addon?

Reproduction

Steps to reproduce:

  • npm init ember-app test-app (npm init ember-app always makes sure it is up-to-date)
  • cd test-app
  • ember install ember-cli-fastboot
  • npm start
  • visit http://localhost:4200
  • 💥

Here is a simple reproduction that has the issue https://github.com/mansona/ember-data-fetch-test but as you can see from the steps it's pretty easy to reproduce

Versions

[email protected] /Users/mansona/temp/test-app
└── [email protected]

[email protected] /Users/mansona/temp/test-app
└── [email protected]

[email protected] /Users/mansona/temp/test-app
└── [email protected]

/cc @rwjblue @runspired as we were looking at this together last time

All 10 comments

We expect users to install ember-fetch when not using jquery or to provide their own alternative fetch.

So as far as I see it, the steps that I ran above should not be breaking the base install. It should work out of the box.

Leaving that aside for the moment this is a brand new non-octane app so jQuery is in fact enabled:

"jquery-integration": true

Interesting, that is a different fail case. I think this needs to be wrapped in a HAS_JQUERY feature flag: https://github.com/emberjs/data/blob/master/packages/adapter/addon/-private/utils/fetch.ts#L16

cc @rwjblue

I can confirm this is still happening in ember 3.12

[email protected] /Users/mansona/temp/three-twelve
└── [email protected]

[email protected] /Users/mansona/temp/three-twelve
└── [email protected]

[email protected] /Users/mansona/temp/three-twelve
└── [email protected]

@mansona i noticed that the https://github.com/mansona/ember-data-fetch-test repository is empty FYI. I can recreate it as well:
Screen Shot 2019-08-20 at 09 44 18

I am also seeing this issue in our Guides app: https://github.com/ember-learn/guides-source/pull/994

@efx whoops sorry! 😂 I must have forgotten to push it in the end

I pushed the recreation for 3.12 to ember-data-fetch-test just now, although as you can see the steps to reproduce are pretty clear 👍

@mansona @runspired It looks like it's rolling up the whole @ember-data/adapter/-private into one file, and that puts the raw https://github.com/emberjs/data/blob/733c8dedc7f7906e782013352a142779e14a544a/packages/adapter/addon/-private/utils/fetch.ts#L7-L18 onto the page.

I think this method should not be run until it's called, rather then run when the @ember-data/adapter/-private package is required.

This is the bit of the compiled js.

  /*
   * Part of the `serializeQueryParams` helper function.
   */

  function add(s, k, v) {
    // Strip out keys with undefined value and replace null values with
    // empty strings (mimics jQuery.ajax)
    if (v === undefined) {
      return;
    } else if (v === null) {
      v = '';
    }

    v = typeof v === 'function' ? v() : v;
    s[s.length] = encodeURIComponent(k) + "=" + encodeURIComponent(v);
  }

  var _fetch = null;

  if (require.has('fetch')) {
    // use `fetch` module by default, this is commonly provided by ember-fetch
    var foundFetch = require__default('fetch').default;

    _fetch = function _fetch() {
      return foundFetch;
    };
  } else if (typeof fetch === 'function') {
    // fallback to using global fetch
    _fetch = function _fetch() {
      return fetch;
    };
  } else {
    throw new Error('cannot find the `fetch` module or the `fetch` global. Did you mean to install the `ember-fetch` addon?');
  }

  var _fetch$1 = _fetch;

  /**
    @module @ember-data/adapter
  */
Was this page helpful?
0 / 5 - 0 ratings