emotion icon indicating copy to clipboard operation
emotion copied to clipboard

Can't resolve '@emotion/react/jsx-runtime'

Open kushagra51 opened this issue 2 years ago • 9 comments

Current behavior:

Scenario: I am creating a package which exports react screens, and that package is then consumed by a container react application that consumes the screens and renders them. But I am getting the below error while running the container application.

Can't resolve '@emotion/react/jsx-runtime' in '/Users/test/Desktop/conatiner-app/node_modules/my-package'
Did you mean 'emotion-react-jsx-runtime.browser.esm.js'?
BREAKING CHANGE: The request '@emotion/react/jsx-runtime' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

Note: The catch here is the error only occurs if I am keeping @emotion/react in peer and devDependencies of the package in order to not include it in the bundle, because I have kept @emotion/react in dependency in the container application.

But if I keep @emotion/react in dependency in my package and as well as in dependency in my container application then it works, but doing this isn't a good idea as I don't want to ship the unnecessary @emotion/react code with my package's bundle.

Bundled code of my package which imports jsx from @emotion/react/jsx-runtime:

Screenshot 2023-11-27 at 1 29 04 AM

Environment information:

  • react version: 17.0.2
  • @emotion/react version: 11.9.3

kushagra51 avatar Nov 26 '23 20:11 kushagra51

Have the same issue, would like a fix

yarv-dev avatar Apr 19 '24 06:04 yarv-dev

Same issue here

AfLosada avatar Apr 22 '24 20:04 AfLosada

+1

dondxniel avatar Apr 24 '24 07:04 dondxniel

+1. I am giving up coding.

arnoob16 avatar Jul 26 '24 09:07 arnoob16

O god, any one with solution?

ullaskunder1 avatar Jul 31 '24 06:07 ullaskunder1

Here is a fix

import react from '@vitejs/plugin-react-swc';
import mdx from '@mdx-js/rollup';
import vike from 'vike/plugin';
import { UserConfig } from 'vite';
import { cjsInterop } from 'vite-plugin-cjs-interop';
import { ParserConfig } from '@swc/core';

export default {
  plugins: [
    vike({
      prerender: true,
    }),
    mdx(),
    react({
      jsxImportSource: '@emotion/react',
      devTarget: 'es2022',
      tsDecorators: true,
      plugins: [['@swc/plugin-emotion', {}]],
      // parserConfig(id): ParserConfig {
      //   let parsed;
      //   if (id.endsWith('.mdx')) parsed = { syntax: 'ecmascript', tsx: true };
      //   return parsed as ParserConfig;
      // },
    }),
    cjsInterop({
      // List of CJS dependencies that require interop
      dependencies: [
        'react-helmet-async',
      ],
    }),
  ],
  resolve: {
    preserveSymlinks: true,
    alias: {
      "@emotion/react/jsx-runtime": "react/jsx-runtime",
    }

  },
} as UserConfig;

geyang avatar Aug 01 '24 05:08 geyang

It's not a fix - it's a workaround that makes using the css prop impossible. I can't say what a fix is since nobody has shared a repro case here

Andarist avatar Aug 01 '24 14:08 Andarist