Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use import statement instead of require #899

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

o-az
Copy link

@o-az o-az commented Dec 29, 2023

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

@philcockfield
Copy link

philcockfield commented Dec 29, 2023

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

@ljharb
Copy link
Contributor

ljharb commented Dec 29, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants