【问题标题】:SvelteKit: Packages not being able to access node functionsSvelteKit:包无法访问节点功能
【发布时间】:2022-08-03 22:06:54
【问题描述】:

我正在使用 SvelteKit 构建一个应用程序并将其发布到 Cloudflare Pages,但我正在使用的一些包无法访问节点本机功能。这是构建日志:

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 工作者支持它,所以我不明白为什么。

    标签: javascript node.js typescript svelte sveltekit


    【解决方案1】:

    我在尝试从 Vercel 迁移到 Cloudflare Pages 时遇到了同样的问题,看起来 Cloudflare Workers(这是 Pages 用于功能的底层)仅支持使用 webpack 或其他 polyfill 捆绑器的 Node.js 包,因为它不是 Node .js。它只在边缘网络上使用 V8 引擎而不是在 Node.js 运行时运行 JavaScript 代码,因此任何具有 Node.js 特定依赖项(如 fs 和 net/http)的 Node.js 包都不兼容。

    Here 是与 Cloudflare Workers 兼容的软件包的列表,here 是有关 Node.js 和 Cloudflare Workers 如何协同工作的进一步阅读。

    要将您的项目部署到 Cloudflare Pages,您需要重构代码以仅使用上述兼容包列表中的 Node.js 包,或者如果不存在兼容包,则使用纯 JavaScript 实现任何功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 2019-05-10
      • 1970-01-01
      相关资源
      最近更新 更多