SvelteKit 无法解决依赖关系,即使它是外部的
SvelteKit could not resolve dependency even though it's external
我正在制作一个用户身份验证页面,就此而言,我正在使用 bcrypt 进行哈希处理。一切都很好,直到我在代码中实际使用 bcrypt 模块。 运行 它在开发模式下工作完全正常,在预览构建时它 运行 完全正常,但在构建过程中发生错误,部署时导致它停止构建并出错。
这是错误:
> Using @sveltejs/adapter-vercel
> 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)
43 │ const AWSMock = require('mock-aws-s3');
╵ ~~~~~~~~~~~~~
> 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)
76 │ const AWS = require('aws-sdk');
╵ ~~~~~~~~~
> 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)
112 │ const nock = require('nock');
╵ ~~~~~~
> Build failed with 3 errors:
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)
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)
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)
Error: Build failed with 3 errors:
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)
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)
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)
at failureErrorWithLog (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1493:15)
at /home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1151:28
at runOnEndCallbacks (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1069:65)
at buildResponseToResult (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1149:7)
at /home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1258:14
at /home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:629:9
at handleIncomingPacket (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:726:9)
at Socket.readFromStdout (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:596:7)
at Socket.emit (node:events:390:28)
at Socket.emit (node:domain:475:12)
我按照错误所说的进行操作,并将这些包标记为外部包,但仍然无法正常工作,这是我的 svelte.config.js
// svelte.config.js
import sveltePreprocess from 'svelte-preprocess';
import makeAttractionsImporter from 'attractions/importer.js';
import vercel from '@sveltejs/adapter-vercel';
import path, { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
// for more information about preprocessors
preprocess: sveltePreprocess({
postcss: true,
scss: {
importer: makeAttractionsImporter({
themeFile: path.join(__dirname, 'static/css/theme.scss')
}),
includePaths: [path.join(__dirname, './static/css')]
}
}),
kit: {
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
adapter: vercel(),
vite: {
optimizeDeps: {
exclude: ['bcrypt']
},
ssr: {
external: ['mock-aws-s3', 'aws-sdk', 'nock', 'node-pre-gyp']
},
resolve: {
alias: {
$lib: resolve('src/lib'),
$components: resolve('src/components'),
$utils: resolve('src/utils')
}
}
}
}
};
export default config;
我开始调查它,问题似乎是由 node-pre-gyp
引起的,所以我也在配置中将其标记为外部,但它仍然产生相同的确切错误。
似乎问题出在 node-pre-gyp
试图解析 bcrypt 的 C++ 二进制文件时,它只在构建时出错,当它被强制 运行 时,它 运行s非常好。
我发现的一个解决方案是只在 package.json 中包含这些依赖项 "nock", "mock-aws-s3", "aws-sdk"
,这将解决问题,但不是预期的方式,因为这意味着捆绑包将更大,因为客户端不需要 3 个额外的依赖项。
希望有更好的解决办法!
这个解决方案的警告是,包的大小当然会更大,我希望它不会大很多。
我在使用 bcrypt 和 argon2 时遇到了同样的问题,因为它们都依赖于 node-pre-gyp 并进行了一些挖掘,并在纯 javascript.
中找到了 bcrypt 的实现
请注意,它很可能比 C 实现慢,但它确实可以在生产 Sveltekit 环境中工作,并在 Vercel 上进行了测试。
这是 bcryptjs 包:https://www.npmjs.com/package/bcryptjs
我有同样的问题,但 @googlemaps/js-api-loader
包。我在 SvelteKit repo https://github.com/sveltejs/kit/issues/928
中找到了关于这个问题的讨论
我通过 updating svelte.config.js
修复了它,如下所示:
vite: {
ssr: {
noExternal: ['@googlemaps/js-api-loader']
}
}
Vercel 已发布 article 关于某些库(尤其是 bcrypt)在本地工作但由于缺少本机依赖项而不在无服务器功能上工作。他们建议要么选择替代包,要么在构建映像中安装必要的共享库。
bcryptjs 对我来说工作得很好。缺点确实是速度较慢(慢30%)。
我正在制作一个用户身份验证页面,就此而言,我正在使用 bcrypt 进行哈希处理。一切都很好,直到我在代码中实际使用 bcrypt 模块。 运行 它在开发模式下工作完全正常,在预览构建时它 运行 完全正常,但在构建过程中发生错误,部署时导致它停止构建并出错。
这是错误:
> Using @sveltejs/adapter-vercel
> 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)
43 │ const AWSMock = require('mock-aws-s3');
╵ ~~~~~~~~~~~~~
> 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)
76 │ const AWS = require('aws-sdk');
╵ ~~~~~~~~~
> 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)
112 │ const nock = require('nock');
╵ ~~~~~~
> Build failed with 3 errors:
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)
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)
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)
Error: Build failed with 3 errors:
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)
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)
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)
at failureErrorWithLog (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1493:15)
at /home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1151:28
at runOnEndCallbacks (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1069:65)
at buildResponseToResult (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1149:7)
at /home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:1258:14
at /home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:629:9
at handleIncomingPacket (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:726:9)
at Socket.readFromStdout (/home/joshgil/Codes/TouchOfClassEvents/node_modules/esbuild/lib/main.js:596:7)
at Socket.emit (node:events:390:28)
at Socket.emit (node:domain:475:12)
我按照错误所说的进行操作,并将这些包标记为外部包,但仍然无法正常工作,这是我的 svelte.config.js
// svelte.config.js
import sveltePreprocess from 'svelte-preprocess';
import makeAttractionsImporter from 'attractions/importer.js';
import vercel from '@sveltejs/adapter-vercel';
import path, { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
// for more information about preprocessors
preprocess: sveltePreprocess({
postcss: true,
scss: {
importer: makeAttractionsImporter({
themeFile: path.join(__dirname, 'static/css/theme.scss')
}),
includePaths: [path.join(__dirname, './static/css')]
}
}),
kit: {
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
adapter: vercel(),
vite: {
optimizeDeps: {
exclude: ['bcrypt']
},
ssr: {
external: ['mock-aws-s3', 'aws-sdk', 'nock', 'node-pre-gyp']
},
resolve: {
alias: {
$lib: resolve('src/lib'),
$components: resolve('src/components'),
$utils: resolve('src/utils')
}
}
}
}
};
export default config;
我开始调查它,问题似乎是由 node-pre-gyp
引起的,所以我也在配置中将其标记为外部,但它仍然产生相同的确切错误。
似乎问题出在 node-pre-gyp
试图解析 bcrypt 的 C++ 二进制文件时,它只在构建时出错,当它被强制 运行 时,它 运行s非常好。
我发现的一个解决方案是只在 package.json 中包含这些依赖项 "nock", "mock-aws-s3", "aws-sdk"
,这将解决问题,但不是预期的方式,因为这意味着捆绑包将更大,因为客户端不需要 3 个额外的依赖项。
希望有更好的解决办法!
这个解决方案的警告是,包的大小当然会更大,我希望它不会大很多。
我在使用 bcrypt 和 argon2 时遇到了同样的问题,因为它们都依赖于 node-pre-gyp 并进行了一些挖掘,并在纯 javascript.
中找到了 bcrypt 的实现请注意,它很可能比 C 实现慢,但它确实可以在生产 Sveltekit 环境中工作,并在 Vercel 上进行了测试。
这是 bcryptjs 包:https://www.npmjs.com/package/bcryptjs
我有同样的问题,但 @googlemaps/js-api-loader
包。我在 SvelteKit repo https://github.com/sveltejs/kit/issues/928
我通过 updating svelte.config.js
修复了它,如下所示:
vite: {
ssr: {
noExternal: ['@googlemaps/js-api-loader']
}
}
Vercel 已发布 article 关于某些库(尤其是 bcrypt)在本地工作但由于缺少本机依赖项而不在无服务器功能上工作。他们建议要么选择替代包,要么在构建映像中安装必要的共享库。
bcryptjs 对我来说工作得很好。缺点确实是速度较慢(慢30%)。