为什么Vite服务器无法启动?

huangapple go评论90阅读模式
英文:

Why won't vite server start?

问题

启动开发服务器时出现错误

启动开发服务器时出现错误:
类型错误: 无法重新定义属性: crypto
    在 Function.defineProperty (<匿名>) (file:///home/apache/svelte-dnd/node_modules/@sveltejs/kit/src/exports/node/polyfills.js:35:10)
    在 installPolyfills (file:///home/apache/svelte-dnd/node_modules/@sveltejs/kit/src/exports/node/polyfills.js:35:10)
    在 dev (file:///home/apache/svelte-dnd/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:29:3)
    在 configureServer (file:///home/apache/svelte-dnd/node_modules/@sveltejs/kit/src/exports/vite/index.js:605:17)
    在 _createServer (file:///home/apache/svelte-dnd/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:63461:30)
    在 async CAC.<anonymous> (file:///home/apache/svelte-dnd/node_modules/vite/dist/node/cli.js:733:24)

我试图在 vite.config.js 中添加 vite-plugin-node-polyfills

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import { nodePolyfills } from "vite-plugin-node-polyfills";

export default defineConfig({
plugins: [
sveltekit(),
nodePolyfills({
// 是否为 node: 协议导入进行填充。
protocolImports: true,
}),
]
});```

英文:

I get an error when I start the dev server


    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)

I was trying to add vite-plugin-node-polyfills to 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,
		}),
	]
});```


</details>


# 答案1
**得分**: 1

EDIT: 这是与Console Ninja扩展的一个版本冲突导致的。升级到高于`v0.0.167`的版本以解决问题。

https://github.com/sveltejs/kit/issues/10252#issuecomment-1612590791

---
*用于记录的原始帖子:*

SvelteKit仓库中的相关问题:

https://github.com/sveltejs/kit/issues/10252

TLDR(太长不看):到目前为止,由于没有更好的修复方法,切换到`node 20`可以解决问题。

<details>
<summary>英文:</summary>

EDIT: it was due to a conflict with a version of the Console Ninja extension. Upgrade to higher than `v0.0.167` to solve.

https://github.com/sveltejs/kit/issues/10252#issuecomment-1612590791

---
*Original post for the record:*


Related issue in SvelteKit repo:

https://github.com/sveltejs/kit/issues/10252

TLDR: so far, for lack of better fix, switching to `node 20` works

</details>



# 答案2
**得分**: 1

问题与SvelteKit相关。
在撰写本文时,尚无稳定的解决方法。
然而,升级Node版本至`v20.3.1`已解决了此问题,包括我自己在内的许多人。

您可以在开发时仅更新本地计算机上的Node至版本20。使用版本16构建正常运行,使用Node 16部署到Docker也正常运行。

(参考链接:https://github.com/sveltejs/kit/issues/10252)

<details>
<summary>英文:</summary>

Issue is related to the sveltekit.
At the time of writing this, there is not solid fix for this issue.
However, upgrading node version to `v20.3.1` resolved the issue for many including myself.

What you can do is update only your local machine for development with node 20. Build with version 16 works fine and deploying to docker with node 16 also works fine.

(ref: https://github.com/sveltejs/kit/issues/10252)

</details>



# 答案3
**得分**: 1

忘记在我的 Dockerfile 中固定 Node 版本,这就是为什么它突然开始构建失败的原因 &#129318;

我的具体错误

#15 1.188 无法从 /app/vite.config.ts 加载配置
#15 1.191 错误解析配置:
#15 1.191 TypeError: 无法重新定义属性: File
#15 1.191 在 Function.defineProperty ()
#15 1.191 在 Object. (/app/node_modules/@babel/core/lib/index.js:7:8)
#15 1.191 在 Module._compile (node:internal/modules/cjs/loader:1241:14)
#15 1.191 在 Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
#15 1.191 在 Module.load (node:internal/modules/cjs/loader:1091:32)
#15 1.191 在 Module._load (node:internal/modules/cjs/loader:938:12)
#15 1.191 在 Module.require (node:internal/modules/cjs/loader:1115:19)
#15 1.191 在 require (node:internal/modules/helpers:130:18)
#15 1.191 在 Object. (/app/node_modules/@babel/core/lib/config/helpers/config-api.js:16:14)


将 Node 固定到 `FROM node:20.3.0` 对我有帮助,修复了问题。

已经有人提到过,但我想包括我的具体错误消息和情况,以便更容易找到解决方案。

<details>
<summary>英文:</summary>

Forgot to pin node version in my Dockerfile, that&#39;s why it started to fail to build out of nowhere &#129318;

My specific error

#15 1.188 failed to load config from /app/vite.config.ts
#15 1.191 error resolving config:
#15 1.191 TypeError: Cannot redefine property: File
#15 1.191 at Function.defineProperty (<anonymous>)
#15 1.191 at Object.<anonymous> (/app/node_modules/@babel/core/lib/index.js:7:8)
#15 1.191 at Module._compile (node:internal/modules/cjs/loader:1241:14)
#15 1.191 at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
#15 1.191 at Module.load (node:internal/modules/cjs/loader:1091:32)
#15 1.191 at Module._load (node:internal/modules/cjs/loader:938:12)
#15 1.191 at Module.require (node:internal/modules/cjs/loader:1115:19)
#15 1.191 at require (node:internal/modules/helpers:130:18)
#15 1.191 at Object.<anonymous> (/app/node_modules/@babel/core/lib/config/helpers/config-api.js:16:14)


pinning node to `FROM node:20.3.0` fixed it for me

Was already stated but wanted to include my specific error message and situation to make finding it easier

</details>



# 答案4
**得分**: 0

不是一个答案,我和一些其他合作者遇到了类似的问题

```bash
TypeError: 无法重新定义属性: 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! 生命周期脚本 `dev` 失败,带有错误:
npm ERR! 错误:命令失败
npm ERR!在工作区位置:@player/web@0.4.2
npm ERR!在位置:/home/<pathToMyRepo>/apps/web
ERROR:命令以错误退出:command (/home/<pathToMyRepo>/apps/web) npm run dev(1)

在一个 turbo/sveltekit/vite 项目中

英文:

Not an answer I and some other collaborators are having a similar issue

TypeError: Cannot redefine property: crypto
    at Function.defineProperty (&lt;anonymous&gt;)
    at installPolyfills (file:///home/&lt;pathToMyRepo&gt;/node_modules/@sveltejs/kit/src/exports/node/polyfills.js:27:10)
    at dev (file:///home/&lt;pathToMyRepo&gt;/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:29:3)
    at configureServer (file:///home/&lt;pathToMyRepo&gt;/node_modules/@sveltejs/kit/src/exports/vite/index.js:600:17)
    at _createServer (file:///home/&lt;pathToMyRepo&gt;/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:63461:30)
    at async CAC.&lt;anonymous&gt; (file:///home/&lt;pathToMyRepo&gt;/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/&lt;pathToMyRepo&gt;/apps/web 
ERROR: command finished with error: command (/home/&lt;pathToMyRepo&gt;/apps/web) npm run dev exited (1)

In a turbo/sveltekit/vite project

答案5

得分: 0

对于许多在Github问题上的人来说,问题是由于使用Console Ninja而引起的 - 升级到最新版本目前已经解决了那些尝试过的人的问题。

英文:

For many on the Github issue, the issue was caused by having Console Ninja - upgrading to the latest version has solved the problem so far for those who have tried.

huangapple
  • 本文由 发表于 2023年6月26日 21:33:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定