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

import styles undefined #4277

Open
HuXin957 opened this issue Aug 24, 2024 · 5 comments
Open

import styles undefined #4277

HuXin957 opened this issue Aug 24, 2024 · 5 comments
Labels

Comments

@HuXin957
Copy link

To reproduce:

import styles from './index.less';

Current behavior:

const Home = () => {
  console.log(styles) //undefined
  return (
    <div className={styles.main}>
    </div>
  );
};
 {
      test: /\.less$/,
      use: [
        NODE_ENV ? 'style-loader' : MiniCssExtractPlugin.loader,
        'css-loader',
        'postcss-loader',
        {
          loader: 'less-loader',
          options: {
            lessOptions: {
              modules: true,
              javascriptEnabled: true,
            },
          },
        },
      ],
    }

Expected behavior:
I hope "className= { styles.main }" can work
first, why is that?
secondly, how to fix it?

Environment information:

  • less version:4.1.3
  • less-loaderversion:11.1.0
  • nodejs version:20.11.1
  • operating system:mac
@HuXin957 HuXin957 added the bug label Aug 24, 2024
Copy link

dosubot bot commented Aug 24, 2024

@HuXin957 Hello there! I'm here to help you with your GitHub issues while waiting for a human maintainer. I'm a bot named Dosu, and I can assist you with bug fixes, answer your questions, and guide you on becoming a contributor.

The issue you're encountering where styles is undefined could be due to a few reasons. Here are some steps to troubleshoot and potentially fix the problem:

  1. CSS Modules Configuration:
    Ensure that CSS Modules are correctly configured in your css-loader. You need to enable CSS Modules explicitly.

    Update your Webpack configuration to include the modules option in css-loader:

    {
      test: /\.less$/,
      use: [
        NODE_ENV ? 'style-loader' : MiniCssExtractPlugin.loader,
        {
          loader: 'css-loader',
          options: {
            modules: true, // Enable CSS Modules
          },
        },
        'postcss-loader',
        {
          loader: 'less-loader',
          options: {
            lessOptions: {
              javascriptEnabled: true,
            },
          },
        },
      ],
    }
  2. Naming Convention:
    Ensure that your class names in the Less file follow the correct naming convention. For example:

    .main {
      // styles
    }
  3. File Extension:
    Make sure the file extension of your Less file is correct and matches the import statement. For example, if your file is named index.less, the import should be:

    import styles from './index.less';
  4. Check for Errors:
    Look for any errors in the console or terminal that might give more insight into why the styles are not being imported correctly.

If these steps do not resolve the issue, there might be a deeper configuration problem or a compatibility issue with the versions of the tools you are using.

To continue talking to Dosu, mention @dosu.

@HuXin957
Copy link
Author

first,thanks. but, that can't solve my problem

@Program-Monkey
Copy link

You may need to pay attention to the version of css-loader .
(尝试把 css-loader 切到 6.x 及以下试试)

@Program-Monkey
Copy link

As the document write : type namedExport = boolean; Default: Depends on the value of the esModule option. If the value of the esModule options is true, this value will also be true, otherwise it will be false.

My configuration does not explicitly specify the esmodule configuration item as true, but when i add log in the node_mdoules/css-loader/utils.js getModulesOptions, it print the namedExport is true. so i get the import variable is undefined.

When I explicitly specify the namedExport field as false, it runs normally.

According to the document, isn't this field set to false by default ?

@HuXin957
Copy link
Author

You may need to pay attention to the version of css-loader . (尝试把 css-loader 切到 6.x 及以下试试)
Yeah,it's a matter of version. thanks,I fixed it

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

No branches or pull requests

2 participants