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

Import error when using in NextJS #206

Closed
Abhimanyu-Jha opened this issue Dec 30, 2022 · 8 comments
Closed

Import error when using in NextJS #206

Abhimanyu-Jha opened this issue Dec 30, 2022 · 8 comments

Comments

@Abhimanyu-Jha
Copy link

Abhimanyu-Jha commented Dec 30, 2022

SyntaxError: Cannot use import statement outside a module at node_modules/mathbox/src/index.js (10)

Hey, trying to run setup this library for use. I've yarn added

  1. "mathbox-react": "^0.0.11-rc",
  2. "mathbox": "2.0.0",
  3. "three": "^0.148.0",

The page seems to open to this error in the mathbox module. Are there any fixes/ workarounds for this?

@ChristopherChudzicki
Copy link
Owner

Are you using any sort of bundler? (Webpack, vite,...). I would not expect ES Modules (import / export syntax) to work well without a bundler.1.

I would recommend using vite..

Footnotes

  1. It is possible to use ESM in <script> tags with module attribute, but it doesn't generally work well (yet) because ESM doesn't support bare name imports.

@Abhimanyu-Jha
Copy link
Author

Abhimanyu-Jha commented Dec 30, 2022 via email

@ChristopherChudzicki
Copy link
Owner

Do you have a repository you can share? I’ve never used NextJS but I’m happy to try and look into it. (Might take me a few days to find the time)

@Abhimanyu-Jha
Copy link
Author

Yeah, I've shared the private repository with you. You can find the relevant code under ./pages/mideo-3.js.
Thanks!

@ChristopherChudzicki
Copy link
Owner

@Abhimanyu-Jha Everything seemed to work fine for me after yarn install and yarn dev:

Screenshot 2022-12-30 at 4 22 35 PM

@Abhimanyu-Jha
Copy link
Author

Yeah, but after you refresh the page, or make any changes in the file and save the file, it it shows the error and there's no way to remove it.

@ChristopherChudzicki
Copy link
Owner

Yeah, but after you refresh the page, or make any changes in the file and save the file, it it shows the error and there's no way to remove it.

My experience was that:

  • loading localhost:3000 and navigating to localhost:3000/mideo-3 always works correctly, even after reloading and saving; this includes hot-reloads where the page automatically reloads when you change source content
  • localhost:3000/mideo-3 directly (or refreshing on that page) did not work

As mentioned, I am not a next-js expert, but comments like this make me wonder if this is related to SSR in NextJS. My understanding is that when loading localhost:3000/mideo-3, NextJS tries to render that page on the server before serving it to you. It seems reasonable to me that mathbox wouldn't work great with SSR since it relies on a bunch of browser APIs.

@ChristopherChudzicki ChristopherChudzicki changed the title SyntaxError: Cannot use import statement outside a module at node_modules/mathbox/src/index.js (10) Import error when using in NextJS Jan 2, 2023
@Abhimanyu-Jha
Copy link
Author

Thanks for the help, this works as expected in vite 👌🏻

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

No branches or pull requests

2 participants