NodeJS 为什么vite服务器无法启动?

avwztpqn  于 2023-06-29  发布在  Node.js
关注(0)|答案(4)|浏览(231)

当我启动开发服务器时,我得到一个错误

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,
        }),
    ]
});```
inb24sb2

inb24sb21#

SvelteKit repo中的相关问题:
https://github.com/sveltejs/kit/issues/10252
TLDR:到目前为止,由于缺乏更好的修复,切换到node 20工作

i2loujxw

i2loujxw2#

问题与sveltekit相关。在写这篇文章的时候,这个问题还没有可靠的解决方案。但是,将节点版本升级到v20.3.1解决了包括我自己在内的许多人的问题。
您可以做的是仅更新本地计算机,以便使用节点20进行开发。使用版本16构建工作正常,使用node 16部署到docker也工作正常。
(ref:https://github.com/sveltejs/kit/issues/10252

0ejtzxu1

0ejtzxu13#

我进入polyfills.js并更改了import { webcrypto as cryptos (instead of crypto) } from 'node:crypto';
文件的完整路径:

file:///home/apache/svelte-dnd/node_modules/@sveltejs/kit/src/exports/node/polyfills.js:35:10
/** @type {Record<string, any>} */
const globals = {
    cryptos, <---- 
    fetch,
    Response,
    Request,
    Headers,
    ReadableStream,
    TransformStream,
    WritableStream,
    FormData,
    File
};

我不知道这是怎么来的,也没有费心去寻找太多关于它的信息,所以对我的解决方案持保留态度。我不知道这是否会导致以后的问题。但我会运行它回来的时候。这至少为我解决了这个问题,我现在可以启动我的开发服务器了。这可能不是最好的解决方案,但它现在有效。

hm2xizp9

hm2xizp94#

不是一个答案我和其他一些合作者也有类似的问题

TypeError: Cannot redefine property: crypto
    at Function.defineProperty (<anonymous>)
    at installPolyfills (file:///home/<pathToMyRepo>/node_modules/@sveltejs/kit/src/exports/node/polyfills.js:27:10)
    at dev (file:///home/<pathToMyRepo>/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:29:3)
    at configureServer (file:///home/<pathToMyRepo>/node_modules/@sveltejs/kit/src/exports/vite/index.js:600:17)
    at _createServer (file:///home/<pathToMyRepo>/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:63461:30)
    at async CAC.<anonymous> (file:///home/<pathToMyRepo>/node_modules/vite/dist/node/cli.js:733:24)
npm ERR! Lifecycle script `dev` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: @player/web@0.4.2 
npm ERR!   at location: /home/<pathToMyRepo>/apps/web 
ERROR: command finished with error: command (/home/<pathToMyRepo>/apps/web) npm run dev exited (1)

在一个turbo/sveltekit/vite项目中

相关问题