Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: fetch is not defined #1784

Closed
istarkov opened this issue Jun 30, 2021 · 4 comments · Fixed by #1804
Closed

ReferenceError: fetch is not defined #1784

istarkov opened this issue Jun 30, 2021 · 4 comments · Fixed by #1804

Comments

@istarkov
Copy link
Contributor

istarkov commented Jun 30, 2021

This change cause fetch is not defined error with sveltekit + node-adapter.

Describe the bug

Build svelte-kit project with node-adapter,
execute with node v14.16.0, it fails with error "ReferenceError: fetch is not defined"

Logs

file:///root/realadvisor/netimmo.web/.build/index.js:1836
  serverFetch: hooks.serverFetch || fetch
                                    ^

ReferenceError: fetch is not defined
    at get_hooks (file:///root/realadvisor/netimmo.web/.build/index.js:1836:37)
    at init (file:///root/realadvisor/netimmo.web/.build/index.js:1681:12)
    at file:///root/realadvisor/netimmo.web/.build/index.js:1900:1
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

To Reproduce

npm init svelte@next my-app # use defaults
cd my-app
npm install @sveltejs/adapter-node@1.0.0-next.29
npm install

change svelte.config.js on

import node from '@sveltejs/adapter-node';

/** @type {import('@sveltejs/kit').Config} */
const config = {
  kit: {
    // hydrate the <div id="svelte"> element in src/app.html
    target: '#svelte',
    adapter: node({
      out: '.build',
    }),
  },
};

export default config;
npm run build
node build
@stalkerg
Copy link
Contributor

stalkerg commented Jun 30, 2021

This fetch was in packages/kit/src/runtime/server/page/load_node.js but now in packages/kit/src/core/dev/index.js hmm.
At least it's working fine for my latest NodeJs.

@stalkerg
Copy link
Contributor

stalkerg commented Jun 30, 2021

This file contain import '../../install-fetch.js'; and inside we do globalThis.fetch = fetch; it's should be enough.
My NodeJS version is 16.3.0

@stalkerg
Copy link
Contributor

stalkerg commented Jun 30, 2021

ok, reproduced and I know how to fix it, one moment

@vfilatov
Copy link

There is a work around while the hotfix above has not been committed yet.

src/hooks.js:

/** @type {import('@sveltejs/kit').ServerFetch} */
export async function serverFetch(request) {
  /*
  if (request.url.startsWith('https://api.yourapp.com/')) {
    // clone the original request, but change the URL
    request = new Request(
      request.url.replace('https://api.yourapp.com/', 'http://localhost:9999/'),
      request
    );
  }
  */

  return fetch(request);
}

GrygrFlzr added a commit that referenced this issue Jul 2, 2021
Rich-Harris pushed a commit that referenced this issue Jul 2, 2021
* fix: Externalize app initialization to adapters

Fixes #1784

* fix(lint)

* chore: spacing changes

* chore: Add todo in adapter-begin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants