GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — chore: enforce explicit `Buffer` import and add lint rule by shivarm · Pull Request #6525 · expressjs/express

Conversation

@shivarm

@shivarm shivarm commented May 22, 2025

Copy link
Copy Markdown
Contributor

explicitly import Buffer using const { Buffer } = require('node:buffer') for clarity and future compatibility, following Node.js best practices.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bjohansebas

bjohansebas commented May 29, 2025

Copy link
Copy Markdown
Member

I'm not sure this is a good practice, Buffer is a global property

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@krzysdz

krzysdz commented May 29, 2025

Copy link
Copy Markdown
Contributor

I'm not sure this is a good practice, Buffer is a global property

Node.js docs say:

While the Buffer class is available within the global scope, it is still recommended to explicitly reference it via an import or require statement.

This sentence replaced the previous

The Buffer class is within the global scope, making it unlikely that one would need to ever use require('buffer').Buffer.

in nodejs/node#39043, but I can't find any arguments for why it is recommended. I think that it's just a cosmetic change for compatibility (or explicit incompatibility) reasons with other environments, but that's just a guess.

shivarm reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bjohansebas

bjohansebas commented May 29, 2025

Copy link
Copy Markdown
Member

@krzysdz Thanks for the references

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bjohansebas
bjohansebas approved these changes May 29, 2025
View reviewed changes
wesleytodd
wesleytodd approved these changes Jun 7, 2025
View reviewed changes

@wesleytodd wesleytodd left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to keep consistency, so if anyone is willing I think we have a bunch of other packages that could use this change.

FWIW, it is a cosmetic change but is still worth it to do because it sets a good example for anyone reading this code to learn from.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

shivarm and mertssmnoglu reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
UlisesGascon
UlisesGascon approved these changes Jun 7, 2025
View reviewed changes
@Phillip9587

Phillip9587 commented Jun 18, 2025

Copy link
Copy Markdown
Member

@shivarm Could you please update all occurrences of Buffer?

@wesleytodd If we want to keep that consistent we should add a lint rule to prevent usage of global Buffer:

rules:
  no-restricted-globals:
    - error
    - name: Buffer
      message: Use `import { Buffer } from "node:buffer"` instead of the global Buffer.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Phillip9587
Phillip9587 requested changes Jun 18, 2025
View reviewed changes

@Phillip9587 Phillip9587 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@wesleytodd

wesleytodd commented Jun 19, 2025

Copy link
Copy Markdown
Member

@wesleytodd If we want to keep that consistent we should add a lint rule to prevent usage of global Buffer:

I am against one off lint rule discussions on principal, but I agree it would be best if we had these things enforced by linting. Since we have an eslint config already in the repo, I am not opposed to adding one as a temporary step until we get a better setup overall. @shivarm you could add this rule so that you can find and fix all the other implicit global usage.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shivarm
chore: enforce explicit Buffer import and add lint rule
bb8ac68
Signed-off-by: Shivam Sharma <[email protected]>
@shivarm
shivarm force-pushed the explicitly-require-statement-buffer branch from 7611815 to bb8ac68 Compare June 20, 2025 16:25
@shivarm

shivarm commented Jun 20, 2025

Copy link
Copy Markdown
Contributor Author

@wesleytodd Done 👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shivarm shivarm changed the title Buffer: keep nodejs best practice chore: enforce explicit Buffer import and add lint rule Jun 20, 2025
Phillip9587
Phillip9587 approved these changes Jun 20, 2025
View reviewed changes
Hardanish-Singh
Hardanish-Singh reviewed Jun 21, 2025
View reviewed changes

@Hardanish-Singh Hardanish-Singh left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bjohansebas
bjohansebas merged commit 98c85eb into expressjs:master Jun 28, 2025
@shivarm
shivarm deleted the explicitly-require-statement-buffer branch June 28, 2025 05:02
@UlisesGascon UlisesGascon mentioned this pull request Nov 25, 2025
Release: 5.2.0 #6920
Merged
@davidshen84 davidshen84 mentioned this pull request Dec 1, 2025
build(deps): bump express from 5.1.0 to 5.2.1 davidshen84/davidshen84.github.io#124
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@wesleytodd wesleytodd wesleytodd approved these changes
@UlisesGascon UlisesGascon UlisesGascon approved these changes
@Phillip9587 Phillip9587 Phillip9587 approved these changes
@bjohansebas bjohansebas bjohansebas approved these changes
+1 more reviewer
@Hardanish-Singh Hardanish-Singh Hardanish-Singh left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@shivarm @bjohansebas @krzysdz @Phillip9587 @wesleytodd @UlisesGascon @Hardanish-Singh
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.