N
Nuxt4d ago
hackal

[nitro] Dynamic require of "crypto" is not supported

I'm trying to import a package in api route.
import { CodeSandbox } from '@codesandbox/sdk'

const runtimeConfig = useRuntimeConfig()
const sdk = new CodeSandbox(runtimeConfig.codeSandboxApiKey);

export default defineEventHandler(async (event) => {
})
import { CodeSandbox } from '@codesandbox/sdk'

const runtimeConfig = useRuntimeConfig()
const sdk = new CodeSandbox(runtimeConfig.codeSandboxApiKey);

export default defineEventHandler(async (event) => {
})
I'm getting this error:
ERROR [nitro] [uncaughtException] Dynamic require of "crypto" is not supported

at <anonymous> (node_modules/@codesandbox/sdk/dist/esm/index.js:11:9)
at node_modules/uuid/dist/cjs/rng.js (node_modules/@codesandbox/sdk/dist/esm/index.js:197:20)
at __require2 (node_modules/@codesandbox/sdk/dist/esm/index.js:14:49)
at node_modules/uuid/dist/cjs/v1.js (node_modules/@codesandbox/sdk/dist/esm/index.js:216:20)
at __require2 (node_modules/@codesandbox/sdk/dist/esm/index.js:14:49)
at node_modules/uuid/dist/cjs/index.js (node_modules/@codesandbox/sdk/dist/esm/index.js:626:19)
at __require2 (node_modules/@codesandbox/sdk/dist/esm/index.js:14:49)
at node_modules/@codesandbox/pitcher-common/dist/Id.js (node_modules/@codesandbox/sdk/dist/esm/index.js:675:18)
at __require2 (node_modules/@codesandbox/sdk/dist/esm/index.js:14:49)
at node_modules/@codesandbox/pitcher-common/dist/index.js (node_modules/@codesandbox/sdk/dist/esm/index.js:15222:16)
ERROR [nitro] [uncaughtException] Dynamic require of "crypto" is not supported

at <anonymous> (node_modules/@codesandbox/sdk/dist/esm/index.js:11:9)
at node_modules/uuid/dist/cjs/rng.js (node_modules/@codesandbox/sdk/dist/esm/index.js:197:20)
at __require2 (node_modules/@codesandbox/sdk/dist/esm/index.js:14:49)
at node_modules/uuid/dist/cjs/v1.js (node_modules/@codesandbox/sdk/dist/esm/index.js:216:20)
at __require2 (node_modules/@codesandbox/sdk/dist/esm/index.js:14:49)
at node_modules/uuid/dist/cjs/index.js (node_modules/@codesandbox/sdk/dist/esm/index.js:626:19)
at __require2 (node_modules/@codesandbox/sdk/dist/esm/index.js:14:49)
at node_modules/@codesandbox/pitcher-common/dist/Id.js (node_modules/@codesandbox/sdk/dist/esm/index.js:675:18)
at __require2 (node_modules/@codesandbox/sdk/dist/esm/index.js:14:49)
at node_modules/@codesandbox/pitcher-common/dist/index.js (node_modules/@codesandbox/sdk/dist/esm/index.js:15222:16)
Any idea what is going wrong here?
2 Replies
kapa.ai
kapa.ai4d ago
Hi @hackal. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The error you're encountering is related to the way Nuxt's server engine, Nitro, handles dependencies and imports. The @codesandbox/sdk package is trying to use a dynamic require for the crypto module, which is not supported in the Nitro environment. This issue is common when trying to use packages that rely on Node.js built-in modules in a Nuxt 3 server-side context. Nitro aims to be platform-agnostic and doesn't support all Node.js built-in modules by default. Here are a few potential solutions you can try: 1. Use a different package or method that doesn't rely on Node.js-specific modules. 2. If you absolutely need to use this package, you can try adding the crypto module to the Nitro externals. Add the following to your nuxt.config.ts:
export default defineNuxtConfig({
nitro: {
externals: {
inline: ['crypto']
}
}
})

export default defineNuxtConfig({
nitro: {
externals: {
inline: ['crypto']
}
}
})

This tells Nitro to include the crypto module in the bundle. 3. If the above doesn't work, you might need to use the package only on the client-side or in a Nuxt plugin that runs only in the browser environment. 4. As a last resort, you could consider using the builder: 'webpack' option in your Nuxt config, as Webpack might handle these dependencies differently. However, this is not recommended unless absolutely necessary, as it may have other implications for your build process. It's worth noting that using packages designed for Node.js environments in a Nuxt server context can sometimes lead to compatibility issues. Always check if there are alternative packages or methods that are more compatible with Nuxt's server-side rendering approach. For more information on Nuxt's server engine and its limitations, you can refer to the Nuxt Server Engine documentation. If none of these solutions work, you might need to reach out to the maintainers of the @codesandbox/sdk package to see if they can provide a version that's compatible with environments like Nitro that don't support dynamic requires of Node.js built-in modules. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@77852149421191168> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?