1

I don't understand the difference in the code below. What's the difference?

A:

const Hoge = require('@foo/hoge');

B:

const {Hoge} = require('@foo/hoge');

thanks

jacky-soup
  • 11
  • 1
  • I noticed, that this is a duplicate after posting my answer https://stackoverflow.com/questions/38660022/curly-brackets-braces-in-node-require-statement – Tracer69 Sep 04 '20 at 09:33
  • see [object destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring) – Getter Jetter Sep 04 '20 at 09:33
  • Does this answer your question? [Curly brackets (braces) in node require statement](https://stackoverflow.com/questions/38660022/curly-brackets-braces-in-node-require-statement) – dawsnap Sep 04 '20 at 09:39

1 Answers1

2

In the first example, Hoge becomes assigned to the module.exports value from the module

// @foo/hoge entry file
module.exports = {
    Hoge: 123
}

// Your file
const Hoge = require("@foo/hoge")
// Hoge = { Hoge: 123 }

In the second example, you take the property Hoge out of the object module.exports

// Your file
const { Hoge } = require("@foo/hoge")
// Hoge = 123
Tracer69
  • 1,050
  • 1
  • 11
  • 26
  • Thank you for your answer! I understand the behavior. Where can I find this in the documentation? It's hard to find the difference in behavior with the symbols ({} ) ... – jacky-soup Sep 04 '20 at 09:52