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

Error: require() of ES Module not supported #75

Open
amirhhashemi opened this issue Sep 7, 2022 · 15 comments
Open

Error: require() of ES Module not supported #75

amirhhashemi opened this issue Sep 7, 2022 · 15 comments

Comments

@amirhhashemi
Copy link

I'm trying to use solid-aria in Astro but I get :

require() of ES Module myproject/node_modules/@solid-aria/focus/dist/cjs/index.js from myproject/node_modules/@solid-
aria/selection/dist/index.cjs not supported. index.js is treated as an ES module file as it is a .js file whose nearest parent package.json 
contains "type": "module" which declares all .js files in that package scope as ES modules. error. 

Any idea why that happens?

I'm getting this error only when using solid-aria so I asked here instead of the Astro server, And it doesn't look like an Astro issue.

Here is a small stackblitz to reproduce. The error is slightly different here but I think it's the same thing.

https://stackblitz.com/edit/github-7cxbsf?on=stackblitz

@fabien-ml
Copy link
Member

@thetarnav maybe it's because the two packages are not built the same way ? In main branch selection is built with tsup whereas focus is built with rollup. In develop all packages use rollup.

@thetarnav
Copy link
Member

I had some weird issues with the package resolution in Astro.
I don't think it's about the build tool, but rather the package.json config.
I think I also "fixed" it by switching to tsup—it requires a simpler export config.
Or maybe the problem is with rollup creating cjs/index.js instead of index.cjs files, which may be interpreted as mjs. not sure
will try to play with it and see what works

by the way, what's the current reason for using rollup? Do solid-aria ships jsx in any packages? Aren't those all primitives?
If the only reason is the @solid-primitives/props package, then we want to resolve it (solidjs-community/solid-primitives#201)

@fabien-ml
Copy link
Member

Yes some packages ship jsx

@thetarnav
Copy link
Member

jsx as like HTML elements?
The import problem was solved by using .cjs for common js exports, (this is why tsup was working) but now I have this familiar fellow:
image

@thetarnav
Copy link
Member

thetarnav commented Sep 7, 2022

what's funny is if I remove solid-aria and use @solid-primitives/props directly, it works.
It only has a problem if it's a "nested dependency"
app <- @solid-aria/focus <- @solid-primitives/props

@amirhhashemi
Copy link
Author

Yeah. Actually, I opened an issue in the Astro repo a few hours ago about that. I thought the problem was @solid-primitives/props so I created a stackblitz with that package installed but it was working fine. Then I installed solid-aria which brought me here.

@amirhhashemi
Copy link
Author

sadly it doesn't seem to be fixed anytime soon.

@amirhhashemi
Copy link
Author

withastro/astro#4652

@fabien-ml
Copy link
Member

jsx as like HTML elements?

The import problem was solved by using .cjs for common js exports, (this is why tsup was working) but now I have this familiar fellow:

image

Yes, the focus package export a FocusScope component for example.

Stupid question, does it works with solid-start or it's broken on all ssr environment ? 🤨

@thetarnav
Copy link
Member

thetarnav commented Sep 7, 2022

I'm too over it for today to test solid-start :P
But it was working there before. I made sure motionone was compatible, and I don't think anything changed since then.

@jchatard
Copy link

jchatard commented Nov 8, 2022

I just encountered the issue using Solid Start (while updating to 0.2.5). I'm using "@solid-aria/overlays": "^0.1.3", which depends on @solid-primitives/props.

@amirhhashemi
Copy link
Author

@jchatard I think solidjs/vite-plugin-solid#68 fixed it. Probably solid-start is not using the latest version of vite-plugin-solid

@jchatard
Copy link

jchatard commented Nov 9, 2022

@ahhshm I created a repro forcing vite-plugin-solid 2.4.0 and it fires the same error in package.json (at the bottom)

https://github.com/jchatard/web_template_is_not_a_function/blob/main/package.json

I get the following error:

An error occured while server rendering /:

	web.template is not a function
TypeError: web.template is not a function
    at Object.<anonymous> (/Users/jchatard/Desktop/web_template/node_modules/.pnpm/@[email protected][email protected]/node_modules/@solid-primitives/props/dist/cjs/index.js:46:33)
    at Module._compile (node:internal/modules/cjs/loader:1119:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
    at Module.load (node:internal/modules/cjs/loader:997:32)
    at Module._load (node:internal/modules/cjs/loader:838:12)
    at Module.require (node:internal/modules/cjs/loader:1021:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/jchatard/Desktop/web_template/node_modules/.pnpm/@[email protected][email protected]/node_modules/@solid-aria/interactions/dist/index.cjs:538:21)
    at Module._compile (node:internal/modules/cjs/loader:1119:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
    at Module.load (node:internal/modules/cjs/loader:997:32)
    at Module._load (node:internal/modules/cjs/loader:838:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:527:24)
    at async nodeImport (file:///Users/jchatard/Desktop/web_template/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-51c4f80a.js:53132:21)
    at async eval (/node_modules/.pnpm/@[email protected][email protected]/node_modules/@solid-aria/overlays/dist/source/createOverlay.js:19:31)
    at async instantiateModule (file:///Users/jchatard/Desktop/web_template/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-51c4f80a.js:53053:9

@jchatard
Copy link

jchatard commented Nov 9, 2022

But adding:

"pnpm" : {
    "overrides": {
      "@solid-primitives/props": "3.0.1", // <-- this guy
      "vite-plugin-solid": "2.4.0"
    }
  }

Now it works.

@thislooksfun
Copy link

thislooksfun commented Dec 22, 2022

I am running into this issue as well. Based on the errors, it seems that since the package.json file has "type": "module" and the cjs exports end in .js, node is trying to load them as if they are ESM files, which is causing it to error. I have opened an issue upstream with rollup-preset-solid: solidjs-community/rollup-preset-solid#12 to have the files generated with the proper extensions.

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

Successfully merging a pull request may close this issue.

5 participants