账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    webpack中can't resolve style-loader
    90
    0

    webpack can't resolve style-loader 配置如下,求解

    package.json配置:

    {
      "name": "webpack-one",
      "version": "1.0.0",
      "description": "webpack practice for first time",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "webpack-dev-server --progress --color --hot --inline"
      },
      "author": "Li",
      "license": "ISC",
      "devDependencies": {
        "babel": "^6.23.0",
        "babel-core": "^6.26.0",
        "babel-loader": "^7.1.2",
        "css-loader": "^0.28.7",
        "file-loader": "^1.1.5",
        "style-loader": "^0.19.0",
        "stylus": "^0.54.5",
        "stylus-loader": "^3.0.1",
        "url-loader": "^0.6.2",
        "webpack": "^3.8.1"
      },
      "dependencies": {
        "webpack-dev-server": "^2.9.4"
      }
    }
    

    webpack.config.js:

    const path = require('path');
    const webpack = require('webpack');
    
    module.exports = {
      entry: {
        admin: __dirname + '/admin/main.js',
        consumer: __dirname + '/consumer/main.js'
      },
      output : {
        path: path.join(__dirname + '/dist'),
        publicPath: '/dist/',
        filename: '[name].[hash].bundle.js'
      },
      module: {
        rules:[{
          test: /\.js$/,
          use: [
            {loader: 'babel-loader'}
          ]
        },{
          test: /\.css$/,
          use: [
            {loader: 'style-loader'},
            {loader: 'css-loader'}
          ]
        }]
      }
    };
    

    main.js:

    require('./index.css')
    
    document.getElementById('content').innerHTML = 'This is Admin Page!!!!';
    

    提示:
    ERROR in ./admin/main.js
    Module not found: Error: Can't resolve 'style-loader' in '/Users/lijialong/coding/webpack-sample'
    @ ./admin/main.js 1:0-22
    @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./admin/main.js

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 水木皿 普通会员 1楼

      The style-loader is a part of the CSS modules ecosystem, which webpack uses to automatically apply styles to your project's HTML files. If you are having trouble resolving the style-loader in your webpack configuration, there could be several reasons:

      1. Missing configuration: Make sure you have correctly installed and configured the style-loader in your webpack configuration file, either by including it in the resolve configuration or by adding it to the module section of the webpack.config.js file.

      Here's an example of how you might include the style-loader in your webpack.config.js file: ```javascript const path = require('path');

      module.exports = { // ... resolve: { extensions: ['.js', '.jsx', '.css', '.less'], alias: { // Add the styles directory here, if necessary 'style': path.resolve(__dirname, 'styles'), }, }, module: { rules: [ { test: /.css$/, use: [ { loader: 'style-loader', }, ], }, ], }, }; `` In this example, we are specifying that thestyledirectory in thestylesdirectory is the alias for thestyle-loader, so we can use thestyle-loaderinstead of thestyle-loader` in our project's modules.

      1. Missing style-loader in your project: Ensure that the style-loader is available in your project's node_modules directory. Make sure you have added the following line to your package.json file: json "styles": "node_modules/your-style-loader/dist/your-style-loader.js" Replace your-style-loader with the name of the style-loader that you installed in your project and dist/ with the appropriate path for the output directory of your style-loader.

      2. Webpack CLI configuration issue: If you are using the webpack CLI instead of webpack itself, ensure that you have correctly configured the style-loader in your CLI's style-loader configuration. Here's an example: ```bash

      Add the following to your style-loader configuration in the CLI's style-loader module:

      module.exports = { // ... use: [ { loader: 'style-loader', options: { sourceMap: true, }, }, ], }; `` This configuration tells webpack to use thestyle-loaderwith source maps, which allows you to map the output of thestyle-loader` to a separate JavaScript file for debugging and performance purposes.

      1. Webpack dependency tree issue: Ensure that you have installed the style-loader in the correct tree. The style-loader is a part of the css-loader and the css-loader is a part of the style-loader and the style-loader is also a part of the postcss-loader. If you have installed the style-loader in the wrong tree, it might not work correctly. In your webpack configuration, make sure that the style-loader is installed in the node_modules tree, which is the root of your project's modules.
      2. Missing style-loader in your development environment: Make sure you have the style-loader installed in your development environment, as it is not included in the build phase of webpack. If you are using a development environment like Node.js or a React project, you might need to install the style-loader globally using npm: bash npm install style-loader Once you have installed the style-loader, you should be able to use it in your webpack configuration without any issues.
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部