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

Vite4 regression - TypeError: Cannot read properties of undefined #11299

Closed
7 tasks done
nitedani opened this issue Dec 10, 2022 · 10 comments
Closed
7 tasks done

Vite4 regression - TypeError: Cannot read properties of undefined #11299

nitedani opened this issue Dec 10, 2022 · 10 comments

Comments

@nitedani
Copy link

nitedani commented Dec 10, 2022

Describe the bug

The only change was going from vite 3 > vite 4. It was working with vite 3 and even vite 2.

The following code throws an error on the server in development:

import { extendTheme } from "@chakra-ui/react";
const theme = extendTheme({})
(node:8) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
TypeError: Cannot read properties of undefined (reading 'extendTheme')
    at eval (/home/projects/llqijrlvr.github/src/entry.js:5:35)
    at async instantiateModule (file://file:///home/projects/llqijrlvr.github/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-ed9cb113.js:53295:9)

Reproduction

https://stackblitz.com/github/nitedani/vite4-bug123

Steps to reproduce

No response

System Info

System:
    OS: Windows 10 10.0.22000
    CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
    Memory: 18.59 GB / 63.92 GB
  Binaries:
    Node: 18.2.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.1.3 - ~\AppData\Roaming\npm\npm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (108.0.1462.46)
    Internet Explorer: 11.0.22000.120

Used Package Manager

pnpm

Logs

No response

Validations

@nitedani nitedani changed the title TypeError: Cannot read properties of undefined Vite4 regression - TypeError: Cannot read properties of undefined Dec 10, 2022
@sapphi-red
Copy link
Member

sapphi-red commented Dec 10, 2022

This is happening because Vite 4 now correctly resolves the exports field.

@chakra-ui/react declares import condition but that does not have a .mjs extension.
https://publint.bjornlu.com/@chakra-ui/[email protected]
This is why the error below is happening.

(node:8) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use node --trace-warnings ... to show where the warning was created)

Closing as this is not a bug in Vite.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Dec 10, 2022
@fredguest
Copy link

fredguest commented Dec 12, 2022

@sapphi-red I just started getting the ES module warning after bumping to the latest Sveltekit which depends on Vite 4, but there's no indication about which package is causing the warning. How can I track down the relevant package?

  VITE v4.0.0  ready in 436 ms

  ➜  Local:   http://localhost:3000/
  ➜  Network: use --host to expose
  ➜  press h to show help
(node:13745) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
10:39:54 AM [vite-plugin-svelte] ssr compile in progress ...
10:39:55 AM [vite-plugin-svelte] ssr compile done.
package	files	  time	  avg
app  	   26	97.2ms	3.7ms

@sapphi-red
Copy link
Member

@fredguest I think using NODE_OPTIONS="--trace-warnings" would show where it happens.

@fredguest
Copy link

I've tried adding export NODE_OPTIONS="--trace-warnings" to my .zprofile and .zshrc.
I've tried running NODE_OPTIONS="--trace-warnings" npm run dev in the console.
I've tried changing the package.json dev script to "dev": "NODE_OPTIONS='--trace-warnings' vite dev".

So far none of that changes the output of the warning at all. Is there another trick I'm missing? Does the vite dev command have it's own flags? I see a few issues that may be related? sveltejs/kit#7781, sveltejs/kit#1144, #9740

@nitedani
Copy link
Author

Why did you decide to stop supporting these incorrect package.json exports? There are so many incorrectly configured packages out there, I think migration will be hard.

@sapphi-red
Copy link
Member

@fredguest Umm, then, I don't have any idea other than trimming imports manually.

@nitedani What we did is not stop supporting incorrect package.json exports. That was never working with Vite. What we changed is that we stopped trying to use CJS files in any cases (#11101).

@fredguest
Copy link

@sapphi-red ok cool no worries, thanks for the help anyway. Ya I was trying to avoid manual process of elimination but it looks like the only option. I'm sure many people will start hitting that indirect ES Module waring at compile time so if there's anyway to pass a more direct warning through Vite it might be worth implementing.

@nitedani
Copy link
Author

nitedani commented Dec 17, 2022

I think this should be considered as a bug in Vite. It stopped working because of a change in Vite. I think #11101 should be revisited.

@vexkiddy
Copy link

this is a comment from another user on a thread that is referenced above, not sure if its worth adding here:

"It seems the fix is in this PR comment.."
#10504 (review)

@hengwangsay
Copy link

i see, i have a same problem for my own project ssr-setup

@github-actions github-actions bot locked and limited conversation to collaborators Jan 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants