webpack imported module is not a constructor

后端 未结 2 1994
情书的邮戳
情书的邮戳 2021-02-06 23:42

I created a small JS module which I intend to make an npm package, but for now is just on GitHub. This module is written in ES6 and SCSS, and is thus relying on webpack and babe

相关标签:
2条回答
  • 2021-02-07 00:27

    If you are not the library author and are having a problem consuming another library, you may be seeing an error like this:

    TypeError: [LIBRARY_NAME]__WEBPACK_IMPORTED_MODULE_3__ is not a constructor
    

    If that's the case, you may be importing the library incorrectly in your code (it may be a problem with default exports). Double check the library docs for usage.

    It may be as simple as changing this:

    import Foo from 'some-library/Foo';
    

    to this:

    import { Foo } from 'some-library';
    
    0 讨论(0)
  • 2021-02-07 00:32

    It is not working because it is missing libraryTarget and library properties. By doing that webpack know which format of module you would like to create, i.e: commonjs (module.exports) or es (export).

    I would do something like:

    ...
      output: {
        path: path.join(__dirname, 'dist'),
        filename: path.join('[name]', 'index.js'),
        library: "my-library",
        libraryTarget: "umd" // exposes and know when to use module.exports or exports.
      },
    ...
    
    0 讨论(0)
提交回复
热议问题