kit
kit copied to clipboard
Using UUID in API routes gives `Cannot read properties of undefined (reading 'v4')`
Describe the bug
I try to use uuid.v4()
in an API route but Sveltekit is giving me this error. Code:
import * as uuid from 'uuid'
import { v4 as uuidv4 } from "uuid";
export const GET = async () => {
// console.log('uuid', uuid.v4())
console.log('uuidv4', uuidv4())
}
Reproduction
https://github.com/ThangHuuVu/sveltekit-uuid-reproduction https://sveltekit-repro-uuid-thvu.vercel.app/
Logs
Cannot read properties of undefined (reading 'v4')
TypeError: Cannot read properties of undefined (reading 'v4')
at GET (/Users/thangvu/repos/github/sveltekit-repro-uuid/src/routes/api/test/+server.js:5:24)
at render_endpoint (file:///Users/thangvu/repos/github/sveltekit-repro-uuid/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/server/endpoint.js:45:26)
at resolve (file:///Users/thangvu/repos/github/sveltekit-repro-uuid/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/server/index.js:398:23)
at async respond (file:///Users/thangvu/repos/github/sveltekit-repro-uuid/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/server/index.js:261:20)
at async file:///Users/thangvu/repos/github/sveltekit-repro-uuid/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:480:22
### System Info
```Shell
System:
OS: macOS 12.5.1
CPU: (10) arm64 Apple M1 Pro
Memory: 85.08 MB / 32.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.0/bin/yarn
npm: 8.15.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
Browsers:
Chrome: 108.0.5359.98
Firefox: 107.0.1
Safari: 15.6.1
Safari Technology Preview: 16.0
Severity
annoyance
Additional Information
No response
I am seeing the same error in my project. The vite build
and vite preview
tasks work, but the dev server shows the error.
I'm seeing this in my hooks.server.js
as well.
Binaries:
Node: 18.2.0 - ~/.nvm/versions/node/v18.2.0/bin/node
npm: 8.11.0 - ~/.nvm/versions/node/v18.2.0/bin/npm
npmPackages:
@sveltejs/adapter-auto: next => 1.0.0-next.91
@sveltejs/adapter-node: 1.0.0-next.100 => 1.0.0-next.100
@sveltejs/kit: next => 1.0.0-next.589
svelte: ^3.54.0 => 3.54.0
uuid: ^9.0.0 => 9.0.0
vite: ^4.0.0 => 4.0.0
Switching to crypto.randomUUID()
seems to be a good workaround/alternative.
this was working for us in hooks.server.ts in "@sveltejs/kit": "1.0.0-next.570", upgrading to 1.0.0 I see the same issue.
... actually it looks like it might be a vite issue, i upgraded only it from previous working state and the issue occurs, from 3.2.4 to 4.0.1.
I went ahead and filed an issue on the vite repo using OP's original details.
this seems to be an issue in vite, tracked at https://github.com/vitejs/vite/issues/11385
This is fixed in vite 4.0.3