SvelteKit 无法解决依赖关系,即使它是外部的

Jos*_*a Q 9 bcrypt sveltekit

我正在制作一个用户身份验证页面,就此而言,我正在使用 bcrypt 进行哈希处理。一切都很好,直到我在代码中实际使用 bcrypt 模块。在开发模式下运行它工作得很好,并且在预览构建时运行得很好,但在构建过程中会发生错误,部署时会导致它停止构建并出错。

\n

这是错误:

\n
> Using @sveltejs/adapter-vercel\n > node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28: error: Could not resolve "mock-aws-s3" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)\n    43 \xe2\x94\x82     const AWSMock = require('mock-aws-s3');\n       \xe2\x95\xb5                             ~~~~~~~~~~~~~\n\n > node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:76:22: error: Could not resolve "aws-sdk" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)\n    76 \xe2\x94\x82   const AWS = require('aws-sdk');\n       \xe2\x95\xb5                       ~~~~~~~~~\n\n > node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23: error: Could not resolve "nock" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)\n    112 \xe2\x94\x82   const nock = require('nock');\n        \xe2\x95\xb5                        ~~~~~~\n\n> Build failed with 3 errors:\nnode_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28: error: Could not resolve "mock-aws-s3" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)\nnode_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:76:22: error: Could not resolve "aws-sdk" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)\nnode_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23: error: Could not resolve "nock" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)\nError: Build failed with 3 errors:\nnode_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28: error: Could not resolve "mock-aws-s3" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)\nnode_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:76:22: error: Could not resolve "aws-sdk" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)\nnode_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23: error: Could not resolve "nock" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)\n    at failureErrorWithLog (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1493:15)\n    at /home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1151:28\n    at runOnEndCallbacks (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1069:65)\n    at buildResponseToResult (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1149:7)\n    at /home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1258:14\n    at /home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:629:9\n    at handleIncomingPacket (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:726:9)\n    at Socket.readFromStdout (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:596:7)\n    at Socket.emit (node:events:390:28)\n    at Socket.emit (node:domain:475:12)\n
Run Code Online (Sandbox Code Playgroud)\n

我按照错误所说的进行操作,并将这些包标记为外部,但它仍然无法工作,这是我的svelte.config.js

\n
// svelte.config.js\nimport sveltePreprocess from 'svelte-preprocess';\nimport makeAttractionsImporter from 'attractions/importer.js';\nimport vercel from '@sveltejs/adapter-vercel';\nimport path, { dirname, resolve } from 'path';\nimport { fileURLToPath } from 'url';\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\n/** @type {import('@sveltejs/kit').Config} */\nconst config = {\n    // Consult https://github.com/sveltejs/svelte-preprocess\n    // for more information about preprocessors\n    preprocess: sveltePreprocess({\n        postcss: true,\n        scss: {\n            importer: makeAttractionsImporter({\n                themeFile: path.join(__dirname, 'static/css/theme.scss')\n            }),\n            includePaths: [path.join(__dirname, './static/css')]\n        }\n    }),\n\n    kit: {\n        // hydrate the <div id="svelte"> element in src/app.html\n        target: '#svelte',\n        adapter: vercel(),\n        vite: {\n            optimizeDeps: {\n                exclude: ['bcrypt']\n            },\n            ssr: {\n                external: ['mock-aws-s3', 'aws-sdk', 'nock', 'node-pre-gyp']\n            },\n            resolve: {\n                alias: {\n                    $lib: resolve('src/lib'),\n                    $components: resolve('src/components'),\n                    $utils: resolve('src/utils')\n                }\n            }\n        }\n    }\n};\n\nexport default config;\n\n
Run Code Online (Sandbox Code Playgroud)\n

我开始研究它,似乎问题是由 引起的node-pre-gyp,所以我也在配置中将其标记为外部,但它仍然产生相同的错误。

\n

看起来问题是当node-pre-gyp尝试解析 bcrypt 的 C++ 二进制文件时,它只在构建时出错,当它被强制运行时,它运行得很好。

\n

小智 4

Vercel 发表了一篇关于一些库(尤其是 bcrypt)在本地工作但由于缺乏本机依赖项而无法在无服务器功能上工作的文章。他们建议选择替代包或在构建映像中安装必要的共享库。

bcryptjs 对我来说工作得很好。缺点确实是速度较慢(慢了30%)。