I'm learning Nodejs and express. I've got some doubts about it, I don't understand which is the best practice for nodejs using import or require
Asked
Active
Viewed 291 times
1
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 29 '22 at 13:23
-
Welcome to Stack Overflow. Please read about [asking questions](https://stackoverflow.com/help/asking). Note that images of text are not accessible, or searchable, and limit the ability of someone trying to help you in that they can't just copy your code and try it, but have to type it in themselves from what may be a hard to read image. – Jason Aller Sep 29 '22 at 15:35
-
`import` is not class based. I don't know where you got this idea. `import` is syntactic - which means that you have to modify the javascript language in order to support it. On the other hand `require()` is framework based. Which means that the module system was implemented in pure javascript code without modifying the language - you just need to implement the require function. – slebetman Sep 29 '22 at 16:02
1 Answers
0
The only "best practice" is what your organization has set.
The only real downfall, is developers on your team not knowing how to write modules for ES6.
I have done countless code reviews where the developer is using exports.func() and then importing it in another file.
Unless your team is seasoned and not new to JS, then pick one and don't combine them. Since you mentioned you are new, stick with CJS.
Reading materials: syntax differences between CJS & ES6 modules

Alan Spurlock
- 107
- 1
- 11