Missing "./runtime" specifier in "@nuxt/image" package
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';
Same here with Nuxt 4.
@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.
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)),
},
})
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"
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 : ''))
}
}
the docs are incorrect because they refer to the upcoming v2 version, which I need to release
This should be fixed now v2 is released