create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

In v5 can't use import * as styles with css modules, bug?

Open mikoleg opened this issue 3 years ago • 11 comments

Hello, after upgrading to v5 I can not use anymore named import such as

import * as styles from './Table.module.css';

This results in the following error

export 'table' (imported as 'styles') was not found in './Table.module.css' (possible exports: default)

Is this a bug? I can not find any workaround.

Thank you for any help.

mikoleg avatar Dec 18 '21 21:12 mikoleg

Thanks for reporting @mikoleg - does import styles from './Table.module.css'; work?

raix avatar Dec 19 '21 15:12 raix

Thank you, yes import styles from works but unfortunately not import * as styles producing breaking changes. Thank you for adding it to the milestone.

mikoleg avatar Dec 19 '21 17:12 mikoleg

we have the same issue. did you find another workaround beside rewriting the imports? has this something to do with the default TS config?

OneCyrus avatar Jan 24 '22 07:01 OneCyrus

Destructing also not working I mean import { style } from style.module.css is not working. It shows the same error export 'style' (imported as 'style') was not found in './styles.module.css' (possible exports: default)

HarishSha avatar Feb 16 '22 15:02 HarishSha

I am getting the same error after upgrading to v5. For now the only workaround I have is importing the css as import styles from "./blah.module.css" and then I make the variables const {styleOne, styleTwo} = styles. Hope an actual fix comes out soon though.

GianniGabriel avatar Mar 08 '22 16:03 GianniGabriel

#12096

receter avatar Mar 09 '22 10:03 receter

Without having looked too deeply, this is likely caused by CRA 4 using css-loader v4, while CRA5 uses v6, where the option defaults have changed.

You can see how in the latest docs (v6), namedExports are set to false by default.

Furthermore, if you do decide to manually set namedExports to true, you are forced to set exportLocalsConvention to camelCaseOnly, meaning you won't be able to import your variables in something like some_class--name.

Regardless, for those that may want to enable Named Exports (which presumably would resolve the OP's problem) anyways, here's the hacky solution I have for the time being to go into your webpack config (e.g. from react-app-rewired) and modify it. You may need to change it depending on the location of the rule options:

  const cssLoaderModules = webpackConfig.module?.rules?.[1]?.oneOf?.[6]?.use?.[1]?.options?.modules;
  if (cssLoaderModules) {
    cssLoaderModules.namedExport = true;
  }

gsccheng avatar May 26 '22 01:05 gsccheng

you can use import classes from 'Table.module.css'

Hassan-98 avatar Jun 10 '22 11:06 Hassan-98

Without having looked too deeply, this is likely caused by CRA 4 using css-loader v4, while CRA5 uses v6, where the option defaults have changed.

You can see how in the latest docs (v6), namedExports are set to false by default.

Furthermore, if you do decide to manually set namedExports to true, you are forced to set exportLocalsConvention to camelCaseOnly, meaning you won't be able to import your variables in something like some_class--name.

Regardless, for those that may want to enable Named Exports (which presumably would resolve the OP's problem) anyways, here's the hacky solution I have for the time being to go into your webpack config (e.g. from react-app-rewired) and modify it. You may need to change it depending on the location of the rule options:

  const cssLoaderModules = webpackConfig.module?.rules?.[1]?.oneOf?.[6]?.use?.[1]?.options?.modules;
  if (cssLoaderModules) {
    cssLoaderModules.namedExport = true;
  }

This didn't work for me. Tried various different options, but with no luck.

dlaskovkodar avatar Jun 21 '22 11:06 dlaskovkodar

Is there a fix for this? I am still facing this issue on v5.0.1 :(

caal-15 avatar Nov 15 '22 14:11 caal-15

I too am having issues with v5.0.1

ccaspanello avatar Apr 23 '23 03:04 ccaspanello