0

I am confused about require and import in node.js.

Some modules use require and some use import can we use only one or the other for all modules?

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    Does this answer your question? [The difference between "require(x)" and "import x"](https://stackoverflow.com/questions/46677752/the-difference-between-requirex-and-import-x) – Clifford Feb 12 '22 at 08:02
  • It is not node.js specific, but rather JavaScript in general. Also the answer is already out there. E.g: https://stackoverflow.com/questions/46677752/the-difference-between-requirex-and-import-x – Clifford Feb 12 '22 at 08:04
  • It's a confusing topic, that is true. But it's also covered in great detail. You should be able to find a lot of material online to study. – The Fool Feb 12 '22 at 08:15

1 Answers1

0

Yes, you can exclusively use import statements to load modules.

You must configure Node to use the ESM loader. The most common method of doing this is setting the "type" field in package.json to "module":

// package.json
{
  "type": "module"
}

See Determining the module system in the docs.

jsejcksn
  • 27,667
  • 4
  • 38
  • 62
  • 1
    This answer does not really resolve the Op's confusion perhaps. Whilst import can do what require does as you say; require cannot replace import. It is not selective. They are not interchangable. I guess what he really needs to know is the difference, and why he might choose to use one over the other. – Clifford Feb 12 '22 at 08:14
  • @Clifford I'm not aware of any reason to use CJS other than in support of legacy codebases or runtime environments with old versions of Node. Are there any? – jsejcksn Feb 12 '22 at 08:16
  • I have no idea. I am only here because I edited the question so the entire question was not in the title. Style police, not a JS expert. I am not familiar with JavaScript or its evolution (or even what CJS is). The duplicate I found was from googling "JavaScript import vs require".That seemed like an obvious search the OP might have used. From that naive stand point, which is perhaps not dissimilar from the OP's, this answer might benefit from more expansion. Maybe not; I am clearly in no position to determine. – Clifford Feb 12 '22 at 08:32
  • The OP's direct question is "_can we use only one or the other for all modules?_", and I attempted to answer that question directly. Definitively, yes: using `import` statements. – jsejcksn Feb 12 '22 at 08:45
  • That was my rewording of his question in the edit (although I think there is no semantic change). Your answer confirms that you can use import exclusively. Implicit in the question is also the question can you use export exclusively (i.e. "one or the other"), and how would you choose?. Since the OP is also "confused", just instructing rather than explaining does little to resolve that part. My point is that I would be interested in understanding this apparent interchangeability and why you might or should use one or the other - that seems implicit in the question to me. – Clifford Feb 12 '22 at 09:21
  • Ah, the original text did have "_why there is too much confusion about require and import_", perhaps changing that to "_I am confused about..._" removed something you might have addressed. It seemed to me that the OP was confused rather than the JS community in general. Perhaps I am wrong? – Clifford Feb 12 '22 at 09:26
  • @Clifford I understand. According to your interpretation, (IMO) the question is simply a duplicate (juxtapositions have been discussed **extensively**). I didn't interpret it that way: I was just trying to answer as directly as possible. – jsejcksn Feb 12 '22 at 09:27