Why won't vite server start?

2.9k Views Asked by At

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

There are 5 best solutions below

0
nostro On

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

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/[email protected] 
npm ERR!   at location: /home/<pathToMyRepo>/apps/web 
ERROR: command finished with error: command (/home/<pathToMyRepo>/apps/web) npm run dev exited (1)

In a turbo/sveltekit/vite project

0
nostro On

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

0
Neel Patel On

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)

0
nostro On

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.

0
Can Rau On

Forgot to pin node version in my Dockerfile, that's why it started to fail to build out of nowhere

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