当我启动开发服务器时,我得到一个错误
error when starting dev server:
TypeError: Cannot redefine property: crypto
at Function.defineProperty (<anonymous>)
at installPolyfills (file:///home/apache/svelte-dnd/node_modules/@sveltejs/kit/src/exports/node/polyfills.js:35:10)
at dev (file:///home/apache/svelte-dnd/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:29:3)
at configureServer (file:///home/apache/svelte-dnd/node_modules/@sveltejs/kit/src/exports/vite/index.js:605:17)
at _createServer (file:///home/apache/svelte-dnd/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:63461:30)
at async CAC.<anonymous> (file:///home/apache/svelte-dnd/node_modules/vite/dist/node/cli.js:733:24)
我尝试将vite-plugin-node-polyfills添加到vite.config.js
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import { nodePolyfills } from "vite-plugin-node-polyfills";
export default defineConfig({
plugins: [
sveltekit(),
nodePolyfills({
// Whether to polyfill `node:` protocol imports.
protocolImports: true,
}),
]
});```
4条答案
按热度按时间inb24sb21#
SvelteKit repo中的相关问题:
https://github.com/sveltejs/kit/issues/10252
TLDR:到目前为止,由于缺乏更好的修复,切换到
node 20
工作i2loujxw2#
问题与sveltekit相关。在写这篇文章的时候,这个问题还没有可靠的解决方案。但是,将节点版本升级到
v20.3.1
解决了包括我自己在内的许多人的问题。您可以做的是仅更新本地计算机,以便使用节点20进行开发。使用版本16构建工作正常,使用node 16部署到docker也工作正常。
(ref:https://github.com/sveltejs/kit/issues/10252)
0ejtzxu13#
我进入polyfills.js并更改了
import { webcrypto as cryptos (instead of crypto) } from 'node:crypto';
文件的完整路径:
我不知道这是怎么来的,也没有费心去寻找太多关于它的信息,所以对我的解决方案持保留态度。我不知道这是否会导致以后的问题。但我会运行它回来的时候。这至少为我解决了这个问题,我现在可以启动我的开发服务器了。这可能不是最好的解决方案,但它现在有效。
hm2xizp94#
不是一个答案我和其他一些合作者也有类似的问题
在一个turbo/sveltekit/vite项目中