image icon indicating copy to clipboard operation
image copied to clipboard

Missing "./runtime" specifier in "@nuxt/image" package

Open X-Cotang opened this issue 1 year ago • 7 comments

When attempting to create a custom image provider with @nuxt/image in a Nuxt 4 project, an import error occurs while trying to import createOperationsGenerator and defineProvider from @nuxt/image/runtime. The error is as follows:

[plugin:vite:import-analysis] Missing "./runtime" specifier in "@nuxt/image" package
D:/Project/my-frontend/providers/my-provider.ts:2:58
1  |  import { joinURL } from "ufo";
2  |  import { createOperationsGenerator, defineProvider } from "@nuxt/image/runtime";
   |                                                             ^
3  |  
4  |  import { useRuntimeConfig } from '#app/nuxt';

X-Cotang avatar Sep 28 '25 02:09 X-Cotang

Same here with Nuxt 4.

davidurco avatar Sep 29 '25 09:09 davidurco

@X-Cotang I guess temp solution would be to add vite.resolve.alias '@nuxt/image/runtime': fileURLToPath(new URL('./node_modules/@nuxt/image/dist/runtime.mjs', import.meta.url)),

or

use v2 alpha where this is fixed already.

davidurco avatar Sep 29 '25 10:09 davidurco

I tried both workarounds, but the error did not resolve in my environment. Instead, I used the following workaround and resolved the error.

import { fileURLToPath } from 'node:url'

export default defineNuxtConfig({
  // other settings...

  alias: {
    '@nuxt/image/runtime': fileURLToPath(new URL('./node_modules/@nuxt/image/dist/runtime', import.meta.url)),
  },
})

EnjoyKojima avatar Oct 10 '25 04:10 EnjoyKojima

Turns out it is not fixed in v2 alpha. I solved it by installing nightly version, here's how it looks in my package.json:

"@nuxt/image": "npm:@nuxt/image-nightly@latest"

davidurco avatar Oct 10 '25 07:10 davidurco

The image module already creates an alias #image that points to the runtime directory, so you should be able to use that instead. Note that defineProvider is no longer included. Here's a simple example. The docs should really be updated to include something like this:

import { joinURL } from 'ufo'
import { createOperationsGenerator } from '#image'
import type { ProviderGetImage } from '@nuxt/image'

const operationsGenerator = createOperationsGenerator()

export const getImage: ProviderGetImage = (src, { modifiers, baseURL }) => {
  if (!baseURL) {
    // also support runtime config
    baseURL = useRuntimeConfig().public.siteUrl
  }

  const operations = operationsGenerator(modifiers)

  return {
    url: joinURL(baseURL, src + (operations ? '?' + operations : ''))
  }
}

gitcarter avatar Oct 16 '25 16:10 gitcarter

the docs are incorrect because they refer to the upcoming v2 version, which I need to release

danielroe avatar Oct 23 '25 11:10 danielroe

This should be fixed now v2 is released

luc122c avatar Nov 05 '25 11:11 luc122c