ai icon indicating copy to clipboard operation
ai copied to clipboard

The package "crypto" wasn't found on the file system but is built into node.

Open Archimagus opened this issue 1 year ago • 1 comments

After installing the "ai" package and "openai-edge" as recommended in the docs vercel can no longer build my applicatyion. I get the following error.

<html>
<body>
<!--StartFragment-->

> Using @sveltejs/adapter-vercel
--
16:16:02.327 | ✘ [ERROR] Could not resolve "crypto"
16:16:02.328 |  
16:16:02.328 | .svelte-kit/output/server/entries/pages/_page.svelte.js:4:19:
16:16:02.328 | 4 │ import crypto from "crypto";
16:16:02.328 | ╵                    ~~~~~~~~
16:16:02.328 |  
16:16:02.328 | The package "crypto" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
16:16:02.328 |  
16:16:02.384 | error during build:
16:16:02.385 | Error: Build failed with 1 error:
16:16:02.385 | .svelte-kit/output/server/entries/pages/_page.svelte.js:4:19: ERROR: Could not resolve "crypto"
16:16:02.385 | at failureErrorWithLog (/vercel/path0/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1636:15)
16:16:02.385 | at /vercel/path0/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1048:25
16:16:02.385 | at /vercel/path0/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:993:52
16:16:02.385 | at buildResponseToResult (/vercel/path0/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1046:7)
16:16:02.386 | at /vercel/path0/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1075:16
16:16:02.386 | at responseCallbacks.<computed> (/vercel/path0/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:697:9)
16:16:02.386 | at handleIncomingPacket (/vercel/path0/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:752:9)
16:16:02.386 | at Socket.readFromStdout (/vercel/path0/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:673:7)
16:16:02.386 | at Socket.emit (node:events:513:28)
16:16:02.386 | at Socket.emit (node:domain:489:12)
16:16:02.407 | ELIFECYCLE  Command failed with exit code 1.
16:16:02.427 | Error: Command "pnpm run build" exited with 1
16:16:02.946 | BUILD_UTILS_SPAWN_1: Command "pnpm run build" exited with

<!--EndFragment-->
</body>
</html>

Archimagus avatar Jun 21 '23 20:06 Archimagus

For info, I ended up "fixing" it by changing

kit: {
		adapter: adapter({
			runtime: "edge",
		}),
	},

to

kit: {
		adapter: adapter({
			runtime: "nodejs18.x",
		}),
      },

Even though the docs claim it to be

Serverless & Edge-ready Build AI-powered apps that are fast and scalable across Node.js, Serverless, and Vercel Edge Functions.

Archimagus avatar Jun 21 '23 21:06 Archimagus

I confirm to have the exact same issue. Thanks for reporting this.

nicolasmendonca avatar Jun 30 '23 04:06 nicolasmendonca

Although in my case the issue isn't fixed by adopting nodejs.18 runtime. I'm getting the following error:

Error: Failed to convert the response to stream. Received status code: 400.
    at AIStream (file:///var/task/.svelte-kit/output/server/entries/endpoints/api/chat/_server.ts.js:229:11)
    at OpenAIStream (file:///var/task/.svelte-kit/output/server/entries/endpoints/api/chat/_server.ts.js:264:10)
    at POST (file:///var/task/.svelte-kit/output/server/entries/endpoints/api/chat/_server.ts.js:301:18)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Which makes sense, given that I'm trying to use openai-edge (you can check the code here).
I'll try adopting openAi and replacing openai-edge. But openai-edge doesn't seem to work with sveltekit at all.

One alternative I haven't tried and could potentially work is to add "crypto" as an external dep. I'll let you know when I have some free time

// +page.ts
import type { Config } from '@sveltejs/adapter-vercel'

export const config: Config = {
  rutime: 'edge',
  external: ['crypto']
}

// And then the declaration of the API route
// export const POST: RouteHandler = () => {}

nicolasmendonca avatar Jun 30 '23 13:06 nicolasmendonca

Nope, declaring crypto as an external module didn't work either.

https://github.com/nicolasmendonca/specialist-gpt/commit/3e255c8a39645c52330508fdc4e853bad2df2e0a

Screenshot 2023-06-30 at 10 50 09

I'm sorry if I'm spamming a bit too much on this issue. I'm just trying to get this fixed.

nicolasmendonca avatar Jun 30 '23 13:06 nicolasmendonca

I had to do

export const config: Config = {
	runtime: "nodejs18.x",
};

in my server.ts file on top of

	kit: {
		adapter: adapter({
			runtime: "nodejs18.x",
		}),
	},

in svelte.config.js

even though I am using openai-edge.

Archimagus avatar Jun 30 '23 14:06 Archimagus

Worked like a charm. Thanks @Archimagus 🙌🏼

Although that is weird. I'd expect all my pages to use the specified nodejs18.x runtime except when a specific route overrides that. So I though I was already using a node runtime for that route 🤔 .

Although I'd still prefer to use vercel edge functions instead of serverless node functions. So let's keep this issue open.

nicolasmendonca avatar Jun 30 '23 18:06 nicolasmendonca

Hi all, I think I've tracked the crypto-importing code to nanoid (randomFillSync). For web it uses the webcrypto API, then for servers it uses node crypto (which isn't available in edge functions). I might search for an alternative and submit a PR to remove this dependency - if anyone has suggestions please let me know!

EDIT: PR created 🚀

lachlancollins avatar Jul 07 '23 19:07 lachlancollins