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

How to use version 3.3.0 (pure ESM)? #336

Closed
adambisek opened this issue Dec 22, 2021 · 7 comments · Fixed by #342
Closed

How to use version 3.3.0 (pure ESM)? #336

adambisek opened this issue Dec 22, 2021 · 7 comments · Fixed by #342
Labels

Comments

@adambisek
Copy link

adambisek commented Dec 22, 2021

I am using razzle project or create-react-app for my projects.

On both I am now as of 3.3.0 getting error like:

...
server.js:4788
module.exports = require("@xstyled/styled-components");
                 ^
Error [ERR_REQUIRE_ESM]: require() of ES Module .../frontend/node_modules/@xstyled/styled-components/dist/index.js from .../frontend/build/server.js 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.
Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in .../frontend/node_modules/@xstyled/styled-components/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
...

To put "type": "module" does not work and I think that even does not make right sense.

How to best deal with this?

@adambisek
Copy link
Author

adambisek commented Dec 23, 2021

After further investigation I found out the cause is in webpack or babel, when compiling, it imports this ESM library the way mentioned above (and therefore it mixes ESM with CJS):

module.exports = require("@xstyled/styled-components");

Please let me this issue opened, if anyone would know how to solve this, I am convinced I am not the last person in the world who is struggling with this.

@AlfonzAlfonz
Copy link
Contributor

After some investigation I have found that it was probably caused by commit 410cd67 and thus it affects versions >=3.2.0. In my case it could be fixed by reverting all packages to version 3.1.2 or 3.1.0 by settings dependecies in package.json and using yarn resolutions:

{
  "dependencies": {
    "@xstyled/core": "3.1.2",
    "@xstyled/emotion": "3.1.2",
    "@xstyled/system": "3.1.2",
    "@xstyled/util": "3.1.0"
  },
  "resolutions": {
    "**/@xstyled/core": "3.1.2",
    "**/@xstyled/system": "3.1.2",
    "**/@xstyled/util": "3.1.0"
  }
}

@SpicyPete
Copy link

@AlfonzAlfonz Thank you for this workaround, I spent a long time stuck on this issue. The resolutions piece is what I was missing 👍

@franzrecinos
Copy link

I came across this issue, thanks @AlfonzAlfonz , I was able to make it work like this:

"dependencies": {
    "@xstyled/styled-components": "3.1.2",
    "next": "12.0.7",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "styled-components": "^5.3.3",
    "swiper": "^7.3.1"
  },
...
"resolutions": {
    "**/@xstyled/core": "3.1.2",
    "**/@xstyled/system": "3.1.2",
    "**/@xstyled/util": "3.1.0"
  }

@sjaakbanaan
Copy link

sjaakbanaan commented Jan 5, 2022

Thanks for the suggestion @AlfonzAlfonz, managed to fix it without resolutions, so just like this:

    "@xstyled/core": "3.1.2",
    "@xstyled/emotion": "3.1.2",
    "@xstyled/system": "3.1.2",
    "@xstyled/util": "3.1.0",
    "@xstyled/styled-components": "3.1.2",

Glad i can fix a whole lot of projects now with this, hopefully this won't be an issue in versions above 3.3.0.

gregberge added a commit that referenced this issue Jan 10, 2022
gregberge added a commit that referenced this issue Jan 10, 2022
@ion-willo
Copy link

We seem to be experiencing the same issue on v3.5.1 - the workaround given by @franzrecinos worked for us

@gregberge
Copy link
Collaborator

We seem to be experiencing the same issue on v3.5.1 - the workaround given by @franzrecinos worked for us

That's very weird, everything should be fixed now.

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

Successfully merging a pull request may close this issue.

7 participants