documentation icon indicating copy to clipboard operation
documentation copied to clipboard

Use import statement instead of require

Open o-az opened this issue 1 year ago • 3 comments

This pull request updates the existing guide to reflect the modern JavaScript module system, transitioning from CommonJS require syntax to ESModules import syntax. With Node.js now offering robust support for ESModules, it is beneficial for new learners to start with the import syntax for several reasons:

  • Future-Proofing: The JavaScript ecosystem has been steadily moving towards ESModules as the standard for quite some time. Learning import syntax prepares developers for up-to-date development practices.
  • Static Analysis: ESModules allow for static analysis of code, enabling tree shaking and other optimization tools that can lead to more efficient bundling and reduced code sizes.
  • Interoperability: ESModules are natively supported in browsers, making the code written for Node.js more consistent with frontend modules, thus simplifying full-stack development.

The changes include:

Replacing require() statements with import declarations. Highlighting 2 ways Node.js can run ESModules, such as using .mjs file extensions or adding "type": "module" in package.json. This update ensures developers have the current up-to-date information on module usage, promoting more efficient, future-proof codebases.

References

o-az avatar Dec 29 '23 07:12 o-az

+1. Yes please. Help new folks do things the new/right way.

philcockfield avatar Dec 29 '23 08:12 philcockfield

Your arguments are flawed. ESM usage is slowly increasing but at a dismally slow rate; CJS is precisely as statically analizable as ESM; and chalk is about ANSI escapes on the command line and is complete nonsensical in a browser, so this change makes things drastically LESS interoperable.

ljharb avatar Dec 29 '23 16:12 ljharb