json-schema-ref-parser icon indicating copy to clipboard operation
json-schema-ref-parser copied to clipboard

`Could not resolve "node-fetch"` error with sveltekit & vite

Open DetachHead opened this issue 2 years ago • 1 comments

routes/+page.svelte

<script lang="ts">
    import { dereference } from "@apidevtools/json-schema-ref-parser";
</script>

svelte.config.js

import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://kit.svelte.dev/docs/integrations#preprocessors
	// for more information about preprocessors
	preprocess: vitePreprocess(),

	kit: {
		adapter: adapter(),
	},
};

export default config;

vite.config.ts

import { sveltekit } from '@sveltejs/kit/vite';
import type { UserConfig } from 'vite';
import path from 'path';

const config: UserConfig = {
	plugins: [sveltekit()]
};

export default config;

output from vite dev

X [ERROR] Could not resolve "node-fetch"

    node_modules/@apidevtools/json-schema-ref-parser/dist/lib/resolvers/http.js:140:113:
      140 │             const { default: fetch, Request, Headers } = yield Promise.resolve().then(() => __importStar(require("node-fetch")));
          ╵                                                                                                                  ~~~~~~~~~~~~

  You can mark the path "node-fetch" as external to exclude it from the bundle, which will remove
  this error. You can also surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.

12:12:43 pm [vite] error while updating dependencies:
Error: Build failed with 1 error:
node_modules/@apidevtools/json-schema-ref-parser/dist/lib/resolvers/http.js:140:113: ERROR: Could not resolve "node-fetch"
    at failureErrorWithLog (C:\Users\user\svelte-schema-form\node_modules\esbuild\lib\main.js:1604:15)
    at C:\Users\user\svelte-schema-form\node_modules\esbuild\lib\main.js:1056:28
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

DetachHead avatar Feb 22 '23 02:02 DetachHead

Any update on this? For now I'm manually including node-fetch as a dependency

joeythomaschaske avatar May 11 '23 18:05 joeythomaschaske

Fixed in v11

jonluca avatar Mar 06 '24 01:03 jonluca