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

fix: require is not defined at dist/register when using vite #476

Closed
yaiks opened this issue May 9, 2021 · 1 comment Β· Fixed by #484
Closed

fix: require is not defined at dist/register when using vite #476

yaiks opened this issue May 9, 2021 · 1 comment Β· Fixed by #484
Assignees
Labels
bug Something isn't working

Comments

@yaiks
Copy link

yaiks commented May 9, 2021

πŸ› The bug
With a fresh install of Nuxt v2.15.3 + nuxt-vite v0.0.37 + @nuxtjs/composition-api v0.23.4 I got the following error:

Screenshot from 2021-05-08 20-22-34

πŸ› οΈ To reproduce
I created a demo repo here. The steps to reproduce the behavior are:

  1. Install the dependencies with yarn or npm i
  2. Run the app using yarn dev or npm run dev
  3. Check the terminal, it will display an onMounted error used during the setup() function
  4. The browser console will display the require error at the dist/register file, just like the screenshot

🌈 Expected behaviour
Expected behavior is to render the Nuxt page correctly, using the variables returned from the setup function

ℹ️ Additional context
I took the liberty to dig a little bit into this issue and found that on src/runtime/composables/index.ts we are importing the register file just like the globals file are being imported on other places.

import '@nuxtjs/composition-api/dist/register'

But the weird thing is that register is not being recognized as a module. I patched the composition-api library and did some experiments locally with the demo repo mentioned above. It turns out that by replacing the import statement above with a relative path:

import "../../register"

the nuxt app runs great and everything works as expected.

The PR #470 seems to modify how the register file is dealt with, and the motivation was the issue #468. However, looking at the issue raised on the vuejs/compostion-api repo, it looks like in Vue3 we cannot access the vm to get the variables from setup() function, and the recommendation is to assert directly in the component. This somehow invalidates the issue #468? πŸ€”

Anyway @danielroe, count on me to debug this issue =) I'll be more than happy to help and provide more insights, information and even open a PR.

@vvnsrzn
Copy link

vvnsrzn commented May 10, 2021

I have the same issue, thanks @yaiks for pointing this out!

danielroe added a commit that referenced this issue May 17, 2021
* addresses issue with .mjs format (nuxt + jest)
* uses `require.resolve` to locate templates
* compile composables into single file
* reverts 16e21eb (#470)
* retains auto-registration for testing (in cjs export - if `process.env.NODE_ENV === 'test'`)

closes #476, closes #479 (by implementing), closes nuxt/vite#134, closes nuxt/vite#123
danielroe added a commit that referenced this issue May 21, 2021
* addresses issue with .mjs format (nuxt + jest)
* compile composables into single file
* registers composition API using webpack entrypoint (or, for vite, by overriding `./middleware.js` import)
* retains auto-registration for testing (in cjs export - if `process.env.NODE_ENV === 'test'`)

closes #476, closes #479 (by implementing)

Also should cover nuxt/vite#134 and nuxt/vite#123.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants