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

HeadlessUI is not working with Nuxt 3 #12996

Closed
bayramorhan opened this issue Dec 9, 2021 · 15 comments
Closed

HeadlessUI is not working with Nuxt 3 #12996

bayramorhan opened this issue Dec 9, 2021 · 15 comments
Labels

Comments

@bayramorhan
Copy link

Environment

Nuxt CLI v3.0.0-27313139.1c88580

Reproduction

It works fine with development but when I build nuxt (nuxi build) and start (yarn start) it gives error. I've tried to import headless ui component from @headlessui/vue/dist/index.js instead of @headlessui/vue but its still same.

Describe the bug

Headless ui is working just fine on development but it gives error after build and trying to browse.

TypeError: Cannot read property 'buttonRef' of undefined
    at setup (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@headlessui\vue\dist\headlessui.cjs.development.js:2622:15)
    at callWithErrorHandling (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:7158:22)
    at setupStatefulComponent (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:6874:29)
    at setupComponent (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:6855:11)
    at renderComponentVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9757:17)
    at renderVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9863:22)
    at renderVNodeChildren (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9878:9)
    at renderElementVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9929:17)
    at renderVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9860:17)
    at renderVNodeChildren (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9878:9)
[Vue warn]: inject() can only be used inside setup() or functional components.
TypeError: Cannot read property 'menuState' of undefined
    at Proxy.render$1 (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@headlessui\vue\dist\headlessui.cjs.development.js:2522:17)
    at renderComponentRoot (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:2015:44)
    at renderComponentSubTree (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9828:51)
    at renderComponentVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9773:16)
    at renderVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9863:22)
    at renderVNodeChildren (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9878:9)
    at renderElementVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9929:17)
    at renderVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9860:17)
    at renderVNodeChildren (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9878:9)
    at renderElementVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9929:17)
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: Unhandled error during execution of watcher callback
TypeError: Cannot read property 'itemsRef' of undefined
    at ReactiveEffect.fn (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@headlessui\vue\dist\headlessui.cjs.development.js:2685:24)
    at ReactiveEffect.run (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@vue\reactivity\dist\reactivity.cjs.js:164:29)
    at ComputedRefImpl.get value [as value] (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@vue\reactivity\dist\reactivity.cjs.js:1075:39)
    at D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@headlessui\vue\dist\headlessui.cjs.development.js:2336:26
    at callWithErrorHandling (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:6615:22)
    at callWithAsyncErrorHandling (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:6624:21)
    at ReactiveEffect.getter [as fn] (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:6970:24)
    at ReactiveEffect.run (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@vue\reactivity\dist\reactivity.cjs.js:164:29)
    at doWatch (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:7078:16)
    at Object.watchEffect (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:6890:12)
[Vue warn]: inject() can only be used inside setup() or functional components.
TypeError: Cannot read property 'menuState' of undefined
    at Proxy.render$1 (D:\projects\headless-test\nuxt3-app\.output\server\node_modules\@headlessui\vue\dist\headlessui.cjs.development.js:2656:17)
    at renderComponentRoot (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:2015:44)
    at renderComponentSubTree (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9828:51)
    at renderComponentVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9773:16)
    at renderVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9863:22)
    at renderComponentSubTree (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9828:13)
    at renderComponentVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9773:16)
    at renderVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9863:22)
    at renderComponentSubTree (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9780:9)
    at renderComponentVNode (file:///D:/projects/headless-test/nuxt3-app/.output/server/chunks/index.mjs:9773:16)

Additional context

No response

Logs

No response

@lookis
Copy link

lookis commented Dec 13, 2021

I use headless ui every day, and it's fine on both dev and release. do you have any code to reproduce the problem?

@bayramorhan
Copy link
Author

bayramorhan commented Dec 13, 2021

I use headless ui every day, and it's fine on both dev and release. do you have any code to reproduce the problem?

I have used this starter but I also tried installing from scratch. You can try it by using this.

https://github.com/productdevbook/nuxt3-app?ref=bestofvue.com

@bayramorhan
Copy link
Author

Reproduction by @vanling

https://codesandbox.io/s/angry-orla-qf02s?file=/pages/index.vue

Opening a new terminal and running npm run build && PORT=4000 node .output/server/index.mjs

@vanling
Copy link

vanling commented Dec 13, 2021

Everything is fine on dev. And it looks ok on production over here, but there is an SSR issue in console. I personally think this is an issue in headlessui, but I am not 100% sure.

@lookis
Copy link

lookis commented Dec 13, 2021

I use headless ui every day, and it's fine on both dev and release. do you have any code to reproduce the problem?

I have used this starter but I also tried installing from scratch. You can try it by using this.

https://github.com/productdevbook/nuxt3-app?ref=bestofvue.com

this repo works fine on my mac, no error

(global) ➜  projects git clone git@github.com:productdevbook/nuxt3-app.git
Cloning into 'nuxt3-app'...
remote: Enumerating objects: 270, done.
remote: Counting objects: 100% (270/270), done.
remote: Compressing objects: 100% (191/191), done.
remote: Total 270 (delta 171), reused 144 (delta 65), pack-reused 0
Receiving objects: 100% (270/270), 231.83 KiB | 472.00 KiB/s, done.
Resolving deltas: 100% (171/171), done.
(global) ➜  projects cd nuxt3-app 
(global) ➜  nuxt3-app git:(master) yarn
yarn install v1.22.17
[1/5] 🔍  Validating package.json...
[2/5] 🔍  Resolving packages...
[3/5] 🚚  Fetching packages...
info There appears to be trouble with your network connection. Retrying...
[4/5] 🔗  Linking dependencies...
warning " > @headlessui/vue@1.4.2" has unmet peer dependency "vue@^3.0.0".
warning " > @heroicons/vue@1.0.5" has unmet peer dependency "vue@>= 3".
warning "@pinia/nuxt > vue-demi@0.12.1" has unmet peer dependency "vue@^3.0.0-0 || ^2.6.0".
warning " > pinia@2.0.3" has unmet peer dependency "vue@^2.6.14 || ^3.2.0".
warning " > sass-loader@12.3.0" has unmet peer dependency "webpack@^5.0.0".
[5/5] 🔨  Building fresh packages...
✨  Done in 43.36s.
(global) ➜  nuxt3-app git:(master) yarn build
yarn run v1.22.17
$ nuxi build
Nuxt CLI v3.0.0-27277330.202617b                                                                                                                                                                                                                                                                                                                                21:58:45
ℹ Vite warmed up in 929ms                                                                                                                                                                                                                                                                                                                                       21:58:48
ℹ Client built in 2609ms                                                                                                                                                                                                                                                                                                                                        21:58:49
ℹ Building server...                                                                                                                                                                                                                                                                                                                                            21:58:49
✔ Server built in 1010ms                                                                                                                                                                                                                                                                                                                                        21:58:50
ℹ Nitro preset is server                                                                                                                                                                                                                                                                                                                                        21:58:50
ℹ Cleaning up .output                                                                                                                                                                                                                                                                                                                                           21:58:50
start Generating public...                                                                                                                                                                                                                                                                                                                                      21:58:50
✔ Generated public .output/public                                                                                                                                                                                                                                                                                                                               21:58:50
start Building server...                                                                                                                                                                                                                                                                                                                                        21:58:50
start Writing server bundle...                                                                                                                                                                                                                                                                                                                                  21:58:54
✔ Server built                                                                                                                                                                                                                                                                                                                                                  21:58:54
  ├─ .output/server/index.mjs (69.5 kB) (23.7 kB gzip)
  ├─ .output/server/chunks/nitro/static.mjs (3.8 kB) (1.4 kB gzip)
  ├─ .output/server/chunks/index.mjs (390 kB) (89.9 kB gzip)
  ├─ .output/server/chunks/app/vue3.mjs (284 B) (200 B gzip)
  ├─ .output/server/chunks/app/server.mjs (202 kB) (42 kB gzip)
  ├─ .output/server/chunks/app/render.mjs (22.5 kB) (6.25 kB gzip)
  └─ .output/server/chunks/app/client.manifest.mjs (1.39 kB) (362 B gzip)
Σ Total size: 3.29 MB (764 kB gzip)
✔ Ready to run node .output/server/index.mjs                                                                                                                                                                                                                                                                                                                    21:58:54
✨  Done in 10.26s.
(global) ➜  nuxt3-app git:(master) yarn start
yarn run v1.22.17
$ node .output/server/index.mjs
Listening on http://localhost:3000

@bayramorhan
Copy link
Author

I use headless ui every day, and it's fine on both dev and release. do you have any code to reproduce the problem?

I have used this starter but I also tried installing from scratch. You can try it by using this.
https://github.com/productdevbook/nuxt3-app?ref=bestofvue.com

this repo works fine on my mac, no error

(global) ➜  projects git clone git@github.com:productdevbook/nuxt3-app.git
Cloning into 'nuxt3-app'...
remote: Enumerating objects: 270, done.
remote: Counting objects: 100% (270/270), done.
remote: Compressing objects: 100% (191/191), done.
remote: Total 270 (delta 171), reused 144 (delta 65), pack-reused 0
Receiving objects: 100% (270/270), 231.83 KiB | 472.00 KiB/s, done.
Resolving deltas: 100% (171/171), done.
(global) ➜  projects cd nuxt3-app 
(global) ➜  nuxt3-app git:(master) yarn
yarn install v1.22.17
[1/5] 🔍  Validating package.json...
[2/5] 🔍  Resolving packages...
[3/5] 🚚  Fetching packages...
info There appears to be trouble with your network connection. Retrying...
[4/5] 🔗  Linking dependencies...
warning " > @headlessui/vue@1.4.2" has unmet peer dependency "vue@^3.0.0".
warning " > @heroicons/vue@1.0.5" has unmet peer dependency "vue@>= 3".
warning "@pinia/nuxt > vue-demi@0.12.1" has unmet peer dependency "vue@^3.0.0-0 || ^2.6.0".
warning " > pinia@2.0.3" has unmet peer dependency "vue@^2.6.14 || ^3.2.0".
warning " > sass-loader@12.3.0" has unmet peer dependency "webpack@^5.0.0".
[5/5] 🔨  Building fresh packages...
✨  Done in 43.36s.
(global) ➜  nuxt3-app git:(master) yarn build
yarn run v1.22.17
$ nuxi build
Nuxt CLI v3.0.0-27277330.202617b                                                                                                                                                                                                                                                                                                                                21:58:45
ℹ Vite warmed up in 929ms                                                                                                                                                                                                                                                                                                                                       21:58:48
ℹ Client built in 2609ms                                                                                                                                                                                                                                                                                                                                        21:58:49
ℹ Building server...                                                                                                                                                                                                                                                                                                                                            21:58:49
✔ Server built in 1010ms                                                                                                                                                                                                                                                                                                                                        21:58:50
ℹ Nitro preset is server                                                                                                                                                                                                                                                                                                                                        21:58:50
ℹ Cleaning up .output                                                                                                                                                                                                                                                                                                                                           21:58:50
start Generating public...                                                                                                                                                                                                                                                                                                                                      21:58:50
✔ Generated public .output/public                                                                                                                                                                                                                                                                                                                               21:58:50
start Building server...                                                                                                                                                                                                                                                                                                                                        21:58:50
start Writing server bundle...                                                                                                                                                                                                                                                                                                                                  21:58:54
✔ Server built                                                                                                                                                                                                                                                                                                                                                  21:58:54
  ├─ .output/server/index.mjs (69.5 kB) (23.7 kB gzip)
  ├─ .output/server/chunks/nitro/static.mjs (3.8 kB) (1.4 kB gzip)
  ├─ .output/server/chunks/index.mjs (390 kB) (89.9 kB gzip)
  ├─ .output/server/chunks/app/vue3.mjs (284 B) (200 B gzip)
  ├─ .output/server/chunks/app/server.mjs (202 kB) (42 kB gzip)
  ├─ .output/server/chunks/app/render.mjs (22.5 kB) (6.25 kB gzip)
  └─ .output/server/chunks/app/client.manifest.mjs (1.39 kB) (362 B gzip)
Σ Total size: 3.29 MB (764 kB gzip)
✔ Ready to run node .output/server/index.mjs                                                                                                                                                                                                                                                                                                                    21:58:54
✨  Done in 10.26s.
(global) ➜  nuxt3-app git:(master) yarn start
yarn run v1.22.17
$ node .output/server/index.mjs
Listening on http://localhost:3000

Did you check the page on the browser? Because it gives error when we try to browse the page.

@vanling
Copy link

vanling commented Dec 13, 2021

@lookis Like i said this might be an issue with headlessui itself

CodeSandbox is to complicated for this simple test

created: https://github.com/vanling/nuxt3-issue-reproduction/
It doesn't get more basic then this npm i && npm run build && npm run start open localhost:3000 and check terminal for errors in server

@lookis
Copy link

lookis commented Dec 13, 2021

Did you check the page on the browser? Because it gives error when we try to browse the page.

OK, i've got this error...

@lookis Like i said this might be an issue with headlessui itself

CodeSandbox is to complicated for this simple test

created: https://github.com/vanling/nuxt3-issue-reproduction/ It doesn't get more basic then this npm i && npm run build && npm run start open localhost:3000 and check terminal for errors in server

I have see that error, but not figure out yet

@yamin90
Copy link

yamin90 commented Jan 11, 2022

in nuxt.config adding

build: { transpile: ['@headlessui/vue'] }

solve problem for now

@thecrypticace
Copy link

We (at Tailwind) looked into this issue this morning and this appears, as far as we can tell, to be a Nuxt 3 issue. I've seen this issue before when Vue got bundled into the dist files (never made it to a release — was an issue encountered during a build tooling refactor).

With that knowledge I poked around to see if there were two versions of Vue included in the server bundle and it appears there are.

  1. Headless UI imports things from the vue package
  2. Which loads (afaict) .output/server/node_modules/vue/index.mjs
  3. Which delegates to .output/server/node_modules/@vue/runtime-core
  4. Yet, the server has an inlined version of Vue in .output/server/chunks/index.mjs which is separate from the one in node_modules

This separate version of Vue has it's own currentInstance / currentRenderingInstance and the mismatch will cause those errors to be displayed / reactivity to not work / etc…. I assume (need to test this myself at some point today to verify this) that the reason adding it to build.transpile fixes it is that Headless UI gets inlined in the server chunk and everything resolves properly.

All in all it seems like this is a Nuxt 3 issue — at least as far as we can tell.

@ilanc
Copy link

ilanc commented Mar 18, 2022

@thecrypticace is right. The problem is exacerbated if you try to use a non-minified version of @headless/ui - then you'll see errors in nuxi dev not only build. The import statements in the dist build of headless ui (i.e. headlessui.esm.js) will include vue imports like so import { inject, provide } from "vue"; - and these will be inlined into .nuxt/dist/server/server.mjs. nuxi will complain about about the duplicate vue function definitions like this:

 WARN  [SSR] Error transforming /code/web/tailwind/headlessui/packages/@headlessui-vue/dist/headlessui.esm.js: Parse failure: Identifier 'inject' has already been declared (192:2)

The prod version of headlessui.esm.js hides these clashes because the function names are mangled:

import{Fragment as Gn,computed as A,defineComponent as ve,h as Ct,inject as _n,nextTick as U,onMounted as Et,onUnmounted as qn,provide as zn,ref as B,toRaw as Q,watch as ze,watchEffect as Qe}from"vue"

@ilanc
Copy link

ilanc commented Mar 22, 2022

I've started a discussion on another headlessui related issue which hasn't seen much love. I'll reference it here seeing as it's headlessui related and hence an important part of the tailwind ecosystem:
https://github.com/nuxt/framework/discussions/3789

@danielroe
Copy link
Member

@thecrypticace is exactly right. At the moment any dependency you have that is importing from vue needs to be added to your build.transpile array so we have the same vue instance. This is technically not a bug but I very much appreciate the inconvenience.

I've opened a new issue to follow the potential solutions, including moving vue to an external: #13632. Thoughts are certainly welcome.

@danielroe
Copy link
Member

We now add vue as an external by default. We can track any remaining issues in #13632.

@FrancoLab
Copy link

@thecrypticace is exactly right. At the moment any dependency you have that is importing from vue needs to be added to your build.transpile array so we have the same vue instance. This is technically not a bug but I very much appreciate the inconvenience.

I've opened a new issue to follow the potential solutions, including moving vue to an external: #13632. Thoughts are certainly welcome.

This worked for me.

build: {
        transpile: ["@heroicons/vue", "@headlessui/vue"],
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants