Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using UUID in API routes gives Cannot read properties of undefined (reading 'v4') #8118

Closed
ThangHuuVu opened this issue Dec 13, 2022 · 5 comments

Comments

@ThangHuuVu
Copy link

ThangHuuVu commented Dec 13, 2022

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

@treppo
Copy link

treppo commented Dec 14, 2022

I am seeing the same error in my project. The vite build and vite preview tasks work, but the dev server shows the error.

@SudoerWithAnOpinion
Copy link

SudoerWithAnOpinion commented Dec 14, 2022

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.

@randyridge
Copy link

randyridge commented Dec 14, 2022

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.

@treppo
Copy link

treppo commented Dec 20, 2022

this seems to be an issue in vite, tracked at vitejs/vite#11385

@randyridge
Copy link

This is fixed in vite 4.0.3

@bluwy bluwy closed this as completed Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants