SvelteKit:无法访问Node函数的软件包

7

我正在使用SvelteKit构建一个应用,并将其发布到Cloudflare Pages,但我使用的某些包无法访问node原生函数。这是构建日志:

2022-07-30T19:05:14.200499Z > Using @sveltejs/adapter-cloudflare
2022-07-30T19:05:14.227308Z ✘ [ERROR] Could not resolve "url"
2022-07-30T19:05:14.227701Z 
2022-07-30T19:05:14.227859Z     node_modules/sequelize/lib/sequelize.js:21:20:
2022-07-30T19:05:14.228009Z       21 │ const url = require("url");
2022-07-30T19:05:14.22817Z           ╵                     ~~~~~
2022-07-30T19:05:14.228388Z 
2022-07-30T19:05:14.228525Z   The package "url" 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.
2022-07-30T19:05:14.228694Z 
2022-07-30T19:05:14.22885Z  ✘ [ERROR] Could not resolve "path"
2022-07-30T19:05:14.229004Z 
2022-07-30T19:05:14.229168Z     node_modules/sequelize/lib/sequelize.js:22:21:
2022-07-30T19:05:14.22932Z        22 │ const path = require("path");
2022-07-30T19:05:14.229452Z          ╵                      ~~~~~~
2022-07-30T19:05:14.229593Z 
2022-07-30T19:05:14.229733Z   The package "path" 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.
2022-07-30T19:05:14.229891Z 
2022-07-30T19:05:14.241501Z ✘ [ERROR] Could not resolve "@firebase/database-compat/standalone"
2022-07-30T19:05:14.2417Z   
2022-07-30T19:05:14.241833Z     node_modules/firebase-admin/lib/app/firebase-namespace.js:106:41:
2022-07-30T19:05:14.242066Z       106 │ ...ject.assign(fn, require('@firebase/database-compat/standalone'));
2022-07-30T19:05:14.242206Z           ╵                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-07-30T19:05:14.242323Z 
2022-07-30T19:05:14.242435Z   The path "./standalone" is not currently exported by package "@firebase/database-compat":
2022-07-30T19:05:14.242596Z 
2022-07-30T19:05:14.243619Z     node_modules/@firebase/database-compat/package.json:16:13:
2022-07-30T19:05:14.244219Z       16 │   "exports": {
2022-07-30T19:05:14.244437Z          ╵              ^
2022-07-30T19:05:14.244574Z 
2022-07-30T19:05:14.244698Z   None of the conditions provided ("types", "node") match any of the currently active conditions ("browser", "default", "require"):
2022-07-30T19:05:14.24483Z  
2022-07-30T19:05:14.244956Z     node_modules/@firebase/database-compat/package.json:27:20:
2022-07-30T19:05:14.24507Z        27 │     "./standalone": {
2022-07-30T19:05:14.245196Z          ╵                     ^
2022-07-30T19:05:14.245368Z 
2022-07-30T19:05:14.2455Z     Consider enabling the "types" condition if this package expects it to be enabled. You can use "conditions: ['types']" to do that:
2022-07-30T19:05:14.245626Z 
2022-07-30T19:05:14.245736Z     node_modules/@firebase/database-compat/package.json:28:6:
2022-07-30T19:05:14.245847Z       28 │       "types": "./dist/database-compat/src/index.standalone.d.ts",
2022-07-30T19:05:14.245969Z          ╵       ~~~~~~~
2022-07-30T19:05:14.24608Z  
2022-07-30T19:05:14.246397Z   You can mark the path "@firebase/database-compat/standalone" 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.
2022-07-30T19:05:14.246547Z 
2022-07-30T19:05:14.282052Z ✘ [ERROR] Could not resolve "util"
2022-07-30T19:05:14.282324Z 
2022-07-30T19:05:14.282497Z     node_modules/retry-as-promised/index.js:3:19:
2022-07-30T19:05:14.282669Z       3 │ var util = require('util');
2022-07-30T19:05:14.282834Z         ╵                    ~~~~~~
2022-07-30T19:05:14.282992Z 
2022-07-30T19:05:14.28312Z    The package "util" 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.
2022-07-30T19:05:14.283505Z 
2022-07-30T19:05:14.317451Z ✘ [ERROR] Could not resolve "url"
2022-07-30T19:05:14.317714Z 
2022-07-30T19:05:14.317856Z     node_modules/pg-connection-string/index.js:3:18:
2022-07-30T19:05:14.317986Z       3 │ var url = require('url')
2022-07-30T19:05:14.318109Z         ╵                   ~~~~~
2022-07-30T19:05:14.318238Z 
2022-07-30T19:05:14.318505Z   The package "url" 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.
2022-07-30T19:05:14.318665Z 
2022-07-30T19:05:14.319392Z ✘ [ERROR] Could not resolve "util"
2022-07-30T19:05:14.319808Z 
2022-07-30T19:05:14.319968Z     node_modules/sequelize/lib/utils/deprecations.js:32:37:
2022-07-30T19:05:14.320241Z       32 │ var import_util = __toModule(require("util"));
2022-07-30T19:05:14.320559Z          ╵                                      ~~~~~~
2022-07-30T19:05:14.320699Z 
2022-07-30T19:05:14.320823Z   The package "util" 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.
2022-07-30T19:05:14.321042Z 
2022-07-30T19:05:14.326057Z ✘ [ERROR] Could not resolve "fs"
2022-07-30T19:05:14.327248Z 
2022-07-30T19:05:14.328892Z     node_modules/sequelize/lib/dialects/sqlite/connection-manager.js:2:19:
2022-07-30T19:05:14.329459Z       2 │ const fs = require("fs");
2022-07-30T19:05:14.329823Z         ╵                    ~~~~
2022-07-30T19:05:14.33005Z  
2022-07-30T19:05:14.330876Z   The package "fs" 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.
2022-07-30T19:05:14.331054Z 
2022-07-30T19:05:14.331281Z ✘ [ERROR] Could not resolve "assert"
2022-07-30T19:05:14.331476Z 
2022-07-30T19:05:14.331604Z     node_modules/sequelize/lib/model.js:21:23:
2022-07-30T19:05:14.33172Z        21 │ const assert = require("assert");
2022-07-30T19:05:14.331829Z          ╵                        ~~~~~~~~
2022-07-30T19:05:14.331936Z 
2022-07-30T19:05:14.33237Z    The package "assert" 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.
2022-07-30T19:05:14.332543Z 
2022-07-30T19:05:14.332673Z ✘ [ERROR] Could not resolve "util"
2022-07-30T19:05:14.332789Z 
2022-07-30T19:05:14.333338Z     node_modules/sequelize/lib/data-types.js:2:21:
2022-07-30T19:05:14.334026Z       2 │ const util = require("util");
2022-07-30T19:05:14.334206Z         ╵                      ~~~~~~
2022-07-30T19:05:14.33441Z  
2022-07-30T19:05:14.334627Z   The package "util" 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.
2022-07-30T19:05:14.334959Z 
2022-07-30T19:05:14.335106Z ✘ [ERROR] Could not resolve "fs"
2022-07-30T19:05:14.335239Z 
2022-07-30T19:05:14.33556Z      node_modules/pg-connection-string/index.js:4:17:
2022-07-30T19:05:14.335971Z       4 │ var fs = require('fs')
2022-07-30T19:05:14.336165Z         ╵                  ~~~~
2022-07-30T19:05:14.336703Z 
2022-07-30T19:05:14.337207Z   The package "fs" 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.
2022-07-30T19:05:14.337373Z 
2022-07-30T19:05:14.337521Z ✘ [ERROR] Could not resolve "fs"
2022-07-30T19:05:14.337657Z 
2022-07-30T19:05:14.337784Z     node_modules/firebase-admin/lib/app/lifecycle.js:21:19:
2022-07-30T19:05:14.337911Z       21 │ const fs = require("fs");
2022-07-30T19:05:14.338056Z          ╵                    ~~~~
2022-07-30T19:05:14.338211Z 
2022-07-30T19:05:14.338342Z   The package "fs" 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.
2022-07-30T19:05:14.338478Z 
2022-07-30T19:05:14.33862Z  ✘ [ERROR] Could not resolve "util"
2022-07-30T19:05:14.338756Z 
2022-07-30T19:05:14.339295Z     node_modules/sequelize/lib/utils/logger.js:59:37:
2022-07-30T19:05:14.339765Z       59 │ var import_util = __toModule(require("util"));
2022-07-30T19:05:14.339936Z          ╵                                      ~~~~~~
2022-07-30T19:05:14.340093Z 
2022-07-30T19:05:14.340237Z   The package "util" 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.


...............


2022-07-30T19:05:14.89587Z  
2022-07-30T19:05:14.896004Z error during build:
2022-07-30T19:05:14.896124Z Error: Build failed with 234 errors:
2022-07-30T19:05:14.896252Z node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js:1:31: ERROR: Could not resolve "stream"
2022-07-30T19:05:14.896371Z node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js:2:25: ERROR: Could not resolve "util"
2022-07-30T19:05:14.896477Z node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js:1:29: ERROR: Could not resolve "events"
2022-07-30T19:05:14.896588Z node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js:2:25: ERROR: Could not resolve "util"
2022-07-30T19:05:14.896701Z node_modules/@fastify/busboy/deps/dicer/lib/PartStream.js:1:25: ERROR: Could not resolve "util"
2022-07-30T19:05:14.896818Z ...
2022-07-30T19:05:14.896942Z     at failureErrorWithLog (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:1621:15)
2022-07-30T19:05:14.897064Z     at /opt/buildhome/repo/node_modules/esbuild/lib/main.js:1263:28
2022-07-30T19:05:14.897441Z     at runOnEndCallbacks (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:1176:65)
2022-07-30T19:05:14.897756Z     at buildResponseToResult (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:1261:7)
2022-07-30T19:05:14.898025Z     at /opt/buildhome/repo/node_modules/esbuild/lib/main.js:1374:14
2022-07-30T19:05:14.898328Z     at /opt/buildhome/repo/node_modules/esbuild/lib/main.js:675:9
2022-07-30T19:05:14.898627Z     at handleIncomingPacket (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:772:9)
2022-07-30T19:05:14.898889Z     at Socket.readFromStdout (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:641:7)
2022-07-30T19:05:14.90122Z      at Socket.emit (node:events:527:28)
2022-07-30T19:05:14.901427Z     at addChunk (node:internal/streams/readable:315:12)
2022-07-30T19:05:14.917314Z Failed: build command exited with code: 1
2022-07-30T19:05:16.243508Z Failed: an internal error occurred

这是在我添加了一些服务器端代码后发生的,可能这就是原因,但Sveltekit通过Cloudflare工作者支持它,所以我不明白为什么。
1个回答

18
我遇到了相同的问题,试图从Vercel迁移到Cloudflare Pages。看起来Cloudflare Workers(Pages在内部使用的函数)仅支持使用webpack或其他polyfill打包程序的Node.js包,因为它不是Node.js。它仅在边缘网络上使用V8引擎运行JavaScript代码,而不是在Node.js运行时中运行,因此任何具有Node.js特定依赖项(如fs和net / http)的Node.js包都不兼容。
这里是一份Cloudflare Workers兼容软件包列表(链接),这里是关于Node.js和Cloudflare Workers如何协同工作的更多阅读资料(链接)
要将项目部署到Cloudflare Pages,您需要重构代码,仅使用上述兼容软件包中的Node.js软件包,或者如果不存在兼容软件包,则使用纯JavaScript实现任何功能。

4
这个答案为我节省了多个小时,谢谢! - Cpp crusaders

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接