首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SvelteKit无法解析依赖项,即使它是外部的

SvelteKit无法解析依赖项,即使它是外部的
EN

Stack Overflow用户
提问于 2021-11-24 13:37:46
回答 3查看 980关注 0票数 3

我正在制作一个用户身份验证页面,在这个问题上,我使用bcrypt进行散列。在我实际使用代码中的bcrypt模块之前,一切都很好。在开发模式下运行它完全正常,并且在预览构建时运行完全正常,但在构建过程中发生错误,当部署时会导致它停止构建并出错。

下面是错误:

代码语言:javascript
运行
复制
> 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

代码语言:javascript
运行
复制
// 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++二进制文件时,它只在构建时出错,当它被强制运行时,它运行得非常好。

EN

回答 3

Stack Overflow用户

发布于 2021-11-24 13:56:11

我找到的一个解决方案是在package.json中包含这些依赖"nock", "mock-aws-s3", "aws-sdk",这将解决问题,但不是预期的方式,因为这意味着包将变得更大,因为客户端中有3个不需要的额外依赖项。

我希望有更好的解决方案!

这个解决方案的警告是,捆绑包的大小当然会更大,我希望它不会太大。

票数 0
EN

Stack Overflow用户

发布于 2021-12-04 13:04:19

我在bcrypt和argon2上遇到了同样的问题,因为它们都依赖于node-pre-gyp,并且做了一些挖掘,发现了一个纯javascript的bcrypt实现。

注意,它很可能比C实现慢,但它确实可以在生产Sveltekit环境中工作,在Vercel上测试。

下面是bcryptjs包:https://www.npmjs.com/package/bcryptjs

票数 0
EN

Stack Overflow用户

发布于 2021-12-15 15:28:18

我也有同样的问题,但是使用@googlemaps/js-api-loader包。我在SvelteKit repo https://github.com/sveltejs/kit/issues/928中找到了关于这个问题的讨论。

我通过updating svelte.config.js修复了它,如下所示:

代码语言:javascript
运行
复制
vite: {
  ssr: {
    noExternal: ['@googlemaps/js-api-loader']
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70097108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档