C
C
colorkid2019-08-24 09:58:16
JavaScript
colorkid, 2019-08-24 09:58:16

Can't see connected module from node_modules. Why?

Hello.
1 - Installed npm module XXX
2 - I include it import XXX from ''XXX';
and when I try to call the initialization of new XXX(), it writes "__WEBPACK_IMPORTED_MODULE_4___default.a is not a constructor",
I print to the console what I have in principle in XXX, and there is an empty object {}
In the docks, there are no words about such a case. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Vyushin, 2019-08-25
@colorkid

Modules may not have default exports. For example, let's say we have a module like this:

// Some module
const a = 1;
const b = 2;

export {
  a,
  b
}

Such a module does not have a default export, so if you try to import it in the following way: there will be an error. To get the export of a module that does not have a default export, you need to use the following construct:
import * as module from 'module';
console.log(module); // {a: 1, b: 2}

the response will be an object containing all the exports of the module.
Getting everything at once is not always advisable. More often it is required to receive something concrete from the module. You can do this in the following way:
import { a } from 'module';
console.log(a); // 1

it is also possible to import multiple values, for example:
import { a, b } from 'module';
console.log(a); // 1
console.log(b); // 2

Specifically in your case, you need to make sure that the module you are trying to import has a default export. That is, you need to look at the source code of the module, or read the documentation for it.
More information about modules can be obtained from the link, I recommend reading:
https://learn.javascript.ru/modules

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question