JetBrains / svg-sprite-loader

Webpack loader for creating SVG sprites.
MIT License
2.02k stars 270 forks source link

TypeError: Cannot read property 'require' of undefined #366

Closed hackerxian closed 4 years ago

hackerxian commented 4 years ago

Do you want to request a feature, report a bug or ask a question? report a bug What is the current behavior?

Module build failed (from /xxx/node_modules/_svg-sprite-loader@4.1.6@svg-sprite-loader/lib/loader.js):
TypeError: Cannot read property 'require' of undefined
    at Object.<anonymous> (/xxxx/node_modules/_svg-sprite-loader@4.1.6@svg-sprite-loader/lib/utils/get-webpack-version.js:2:37)

My webpack config

{
        test: /\.svg$/,
        use: [
          {
            loader: require.resolve('svg-sprite-loader'),
            options: {
              symbolId: 'icon-[name]',
            },
          }
        ],
      }
import './icons/prompt.svg';

If this is a feature request, what is motivation or use case for changing the behavior?

Please tell us about your environment:

Other information get-webpack-version.js use require replace require.main, it works.

rblock-aw commented 4 years ago

Stumbled on a different instantiation of the same bug: due to the implementation of get-webpack-version.js, the webpack configuration file setting in PyCharm (and I'm guessing WebStorm) is failing. Ironically, all related software is developed by JetBrains. Changing the require.main by require in the referenced file fixes the problem for me.

xidb commented 4 years ago

Made a PR for that here https://github.com/JetBrains/svg-sprite-loader/pull/382

kisenka commented 4 years ago

Should be fixed in svg-sprite-loader@4.2.6