NPM package is undefined when entirely imported but works fine when destructured

Viewed 451

Im having this strange problem where importing the textile/hub package in node then destructuring it later the program gives an error, but when I destructure while importing the code doesn't error at all. Does anyone know why this is happening, destructuring while importing works fine for the program but I'm still curious.

import textileHub from '@textile/hub';
const { Client, ThreadID, PrivateKey } = textileHub;
// This fails saying that textileHub is undefined


import { Client, ThreadID, PrivateKey } from '@textile/hub';
// this works perfectly
1 Answers

Those two import syntaxes aren't equivalent- the first one will assign whatever the "default" export from the package is to the variable textileHub. If there isn't a default export (which would have been exported with the syntax export default value), your textileHub variable will be undefined.

The second version directly imports those three named imports. The equivalent "get all named imports as one object before destructuring later" syntax would be

   import * as textileHub from '@textile/hub';
   const { Client, ThreadID, PrivateKey } = textileHub;
Related