Unable to run using Nuxt 3
Hello,
I have a nuxt 3 project that I was trying to integrate puter.js into.
I am getting the following error upoin initializing TypeError: s3.unref is not a function preventing the app from running. It looks like it fails when trying to create a socket connection.
Node version 22.20.0
nuxt version 3.19.2
/* usePuterApiComposable.ts */
import { puter } from '@heyputer/puter.js' // fails here at import
interface PuterAIOptions {}
export function usePuterApi() {
async function generateImage(
prompt: string,
options: PuterAIOptions = {testMode: true},
) {
if (!import.meta.client) {
return // run client side only
}
try {
// Build options object
const aiOptions: Record<string, any> = {}
if (options.model) {
aiOptions.model = options.model
}
if (options.quality) {
aiOptions.quality = options.quality
}
// Call Puter AI txt2img
let imageElement: HTMLImageElement
try {
if (options.testMode) {
imageElement = await puter.ai.txt2img(prompt, true)
} else if (Object.keys(aiOptions).length > 0) {
imageElement = await puter.ai.txt2img(prompt, aiOptions)
} else {
imageElement = await puter.ai.txt2img(prompt)
}
} catch (e) {
console.log('e ', e)
throw e
}
}
return {
generateImage,
}
}
}
this is triggered by a button click which passes the prompt text
- Create a client-only plugin so the module is never evaluated on the server. plugins/puter.client.ts
export default defineNuxtPlugin(async (nuxtApp) => { // runs only on client because of .client.ts const { puter } = await import('@heyputer/puter.js'); nuxtApp.provide('puter', puter); });
- Use it in your composable (no direct top-level import): interface PuterAIOptions { model?: string; quality?: string; testMode?: boolean; }
export function usePuterApi() { const { $puter } = useNuxtApp(); // injected by the plugin
async function generateImage(prompt: string, options: PuterAIOptions = { testMode: true }) { if (!process.client) return; // guard SSR
const aiOptions: Record<string, any> = {};
if (options.model) aiOptions.model = options.model;
if (options.quality) aiOptions.quality = options.quality;
return options.testMode
? await $puter.ai.txt2img(prompt, true)
: Object.keys(aiOptions).length > 0
? await $puter.ai.txt2img(prompt, aiOptions)
: await $puter.ai.txt2img(prompt);
}
return { generateImage }; }
Use
hi @palfaro91 , thanks for reporting the issue
i can confirm that the issue is reproducible on my end, we'll take a look at this 🫡
@hassan689 , thanks for sharing this, will give this a try
Hi @hassan689 thanks for the tip. We have set ssr: false in the nuxt.config.ts file so it's not rendering server side. I also tried dynamically loading the puter instance after the check for import.meta.client to be on the safe side but it would still fail with the same error. . The code looked something like:
interface PuterAIOptions {}
export function usePuterApi() {
let puterInstance: any
async function generateImage(
prompt: string,
options: PuterAIOptions = { testMode: true },
) {
if (!import.meta.client) {
return // run client side only
}
try {
const puter = await getPuter()
// Build options object
const aiOptions: Record<string, any> = {}
if (options.model) {
aiOptions.model = options.model
}
if (options.quality) {
aiOptions.quality = options.quality
}
// Call Puter AI txt2img
let imageElement: HTMLImageElement
try {
if (options.testMode) {
imageElement = await puter.ai.txt2img(prompt, true)
} else if (Object.keys(aiOptions).length > 0) {
imageElement = await puter.ai.txt2img(prompt, aiOptions)
} else {
imageElement = await puter.ai.txt2img(prompt)
}
} catch (e) {
console.log('e ', e)
throw e
}
} catch (e) {
console.log('something went wrong ', e)
}
return {
generateImage,
}
}
async function getPuter() {
if (puterInstance) {
return puterInstance
}
const mod = await import('@heyputer/puter.js')
puterInstance = mod.default || mod
return puterInstance
}
}
I also added it to vite.optimizeDeps.exclude in nuxt.config.ts. and I received a different error: `SyntaxError: The requested module '/_nuxt/node_modules/@heyputer/putility/index.js?v=b60f17a6' does not provide an
export named 'AdvancedBase'.
@reynaldichernando thanks for looking into it 🫡
Hi @palfaro91 You’re still hitting the server/Node bundle of @heyputer/puter.js, so socket.io-client is calling .unref() (Node-only) and blowing up in the browser. We need to force Nuxt to load only the browser bundle, and keep Vite/Nitro from touching it in SSR/prebundle.
Do this:
- Client-only plugin, import the browser build
Create plugins/puter.client.ts:
export default defineNuxtPlugin(async (nuxtApp) => { // runs only on client because of .client.ts
const mod = await import('@heyputer/puter.js/dist/puter.js'); // browser build
const puter = mod.puter ?? mod.default ?? mod;
nuxtApp.provide('puter', puter);});
2)In nuxt.config.ts dont do the preoptimzation
export default defineNuxtConfig({
ssr: false, // you already set this, keep it
vite: {
optimizeDeps: {
exclude: ['@heyputer/puter.js', '@heyputer/putility'],
},
ssr: {
external: ['@heyputer/puter.js', '@heyputer/putility'], // don’t SSR-evaluate <---------
},
},
});
- In your pureAIoption file
const { $puter } = useNuxtApp(); // injected by plugin
( This one is a shortcut but I will not recommend this one just for debugging purpose :) )
vite: {
define: {
'process.browser': 'true',
'process.env': '{}'
}
}
I hope this one helps :)
Hi @hassan689 I just tried that and it continues to fail with the same errors. Did it end up working for you? I literally just copied and pasted your suggestions but no dice.
@Salazareo @reynaldichernando can one of you volunteer to take this on?
thanks for the ping, i can take a look at this again
hi folks, sorry that this took so long, i finally revisited this issue and can confirm it's possible to use puterjs in Nuxt 3
here's a sample repo https://github.com/reynaldichernando/nuxt-puter
the code for nuxt plugin
import type { Puter } from '@heyputer/puter.js'
export default defineNuxtPlugin(async () => {
const g = globalThis as any
const savedProcess = g.process
g.process = undefined
let puter: Puter
try {
puter = (await import('@heyputer/puter.js')).default
} finally {
g.process = savedProcess
}
return {
provide: { puter },
}
})
declare module '#app' {
interface NuxtApp {
$puter: Puter
}
}
this is the only thing needed, and you just import it to your vue components
const { $puter } = useNuxtApp()
hot reload works, production build also works